[Python-Dev] type(obj) vs. obj.class (original) (raw)
Serhiy Storchaka storchaka at gmail.com
Sat Oct 17 18:29:34 EDT 2015
- Previous message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Next message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 18.10.15 00:45, Eric Snow wrote:
In a recent tracker issue about OrderedDict [1] we've had some discussion about the use of type(od) as a replacement for od.class. It came up because the pure Python implementation of OrderedDict uses self.class in 3 different methods (repr, reduce, and copy). The patch in that issue changes the C implementation to use PyTYPE(). [2] So I wanted to get some feedback on the practical implications of such a change and if we need to clarify the difference more formally.
In this specific case [3] there are 3 questions: * Should repr() for a stdlib class use type(self).name or self.class.name? * Should reduce() return type(self) or self.class? * Should copy() use type(self) or self.class? The more general question of when we use type(obj) vs. obj.class applies to both the language and to all the stdlib as I expect consistency there would result in fewer surprises. I realize that there are some places where using obj.class makes more sense (e.g. for some proxy support). There are other places where using type(obj) is the way to go (e.g. special method lookup). However, the difference is muddled enough that usage is inconsistent in the stdlib. For example, C-implemented types use PyTYPE() almost exclusively. 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". -eric
[1] http://bugs.python.org/issue25410 [2] I'm going to open a separate thread about the issue of compatibility and C accelerated types. [3] https://hg.python.org/cpython/file/default/Lib/collections/init.py#l238
Want to add that in common case type(obj) is the same as obj.class. When you set obj.class (assignment is restricted by issue24912), type(obj) is changed as well. You can make obj.class differ from type(obj) if set class as class attribute at class creation time, or made class a property, or like.
class A: pass ... class B: class = A ... type(B()) <class '__main__.B'> B().class <class '__main__.A'>
The only places where obj.class made different from type(obj) in the stdlib, besides tests, are mock object (hence related to tests), and proxy stream in xml.sax.saxutils (I'm not sure that the latter use is correct).
About pickling. Default implementation of reduce_ex uses obj.class in protocols 0 and 1, and type(obj) in protocols 2+.
B().reduce_ex(1) (<function _reconstructor at 0xb705965c>, (<class '__main__.A'>, <class 'object'>, None)) B().reduce_ex(2) (<function __newobj__ at 0xb70596ec>, (<class '__main__.B'>,), None, None, None)
But pickler rejects classes with mismatched type(obj) and obj.class in protocols 2+.
pickle.dumps(B(), 2) Traceback (most recent call last): File "", line 1, in _pickle.PicklingError: args[0] from newobj args has the wrong class
- Previous message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Next message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]