[Python-Dev] type(obj) vs. obj.class (original) (raw)

Martin Panter vadmium+py at gmail.com
Sun Oct 18 04:07:07 EDT 2015


On 18 October 2015 at 05:55, Steven D'Aprano <steve at pearwood.info> wrote:

On Sat, Oct 17, 2015 at 03:45:19PM -0600, Eric Snow wrote:

So, would it make sense to establish some concrete guidelines about when to use type(obj) vs. obj.class? If so, what would those be? It may also be helpful to enumerate use cases for "type(obj) is not obj.class". I for one would like to see a definitive explanation for when they are different, and when you should use one or the other. The only obvious example I've seen is the RingBuffer from the Python Cookbook: http://code.activestate.com/recipes/68429-ring-buffer/

It looks like this example just assigns to the existing class attribute, to switch to a different class. I haven’t seen this ability mentioned in the documentation, but I suspect it is meant to be supported. However assigning to class like that should automatically update the type() return value, so type(ring_buffer) == ring_buffer.class is still maintained.

Perhaps some of this confusion comes from Python 2. I don’t know the details, but I know in Python 2, type() can do something different, so you have to use class directly if you want to be compatible with Python 2 classes. But in Python 3 code I prefer using direct function calls like type() to “special attributes” like class where practical.

The documentation says that * names are reserved for Python and its built-in library, rather than user code. So user code that creates a class attribute or property called class is asking for trouble IMO, and we shouldn’t spend much effort accommodating such cases.

For repr() I would use type(), which seems to agree with what object.repr() uses.



More information about the Python-Dev mailing list