This question already has answers here:
Instance is an "object", but class is not a subclass of "object": how is this possible?
(3 answers)
isinstance() and issubclass() return conflicting results
(4 answers)
Closed 5 years ago.
In Python 2, why are instances of old style classes still instances of object even when they do not explicitly inherit from object?
class OldClass:
pass
>>> isinstance(OldClass(), object)
True
Before testing this, I would have concluded that isinstance(x, object) == True would imply that x is an instance of a subclass of object and thus an instance of a new style class, but it appears that all objects in Python 2 are instances of object (yes, I know how obvious that sounds).
Digging around further, I found some other seemingly odd behavior:
>>> issubclass(OldClass, object)
False
I was under the impression that isinstance(x, SomeClass) is virtually equivalent to issubclass(x.__class__, SomeClass), but apparently I'm missing something.
Related
This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?
This question already has answers here:
Can't set attributes on instance of "object" class
(7 answers)
Closed 5 years ago.
I recently noticed in Python that if I create a dict subclass I can add attributes, however this isn't the case with instances of dict. I.E:
class aDict(dict):
pass
myDict = aDict({"name" : "A"})
myDict.color = "red"
print(myDict.color) #"red"
myOtherDict = dict({"name" : "B"})
myOtherDict.color = "green" #"AttributeError: 'dict' object has no attribute color"
You are interacting with myDict's
__dict__ which is the attribute dictionary given to an object, this is different to the myOtherDict which is the inbuilt dictionary. You can see the difference with how you interact with these by trying:
dir(myDict)
dir(myOtherDict)
and you can see what is accessible via myDict's dict attribute (and compare it with the lack of attribute on myOtherDict via vars()
vars(myDict)
vars(myOtherDict)
There's some more information available here and here that helped me to understand your problem.
This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 5 years ago.
In Python 2, classes should explicitly be defined as subclasses of object. In Python 3, this will be the
default.
>>> class A(object):
pass
>>> class B():
pass
>>> type(B)
<type 'classobj'>
>>> type(A)
<type 'type'>
I use Python 2.7 and as I know in 2.7 class inherits from object.
That is a so-called "new style object", introduced in python 2.2.
New style objects have a different object model to classic objects, and some things won't work properly with old style objects, for instance, super(), #property and descriptors.
More on it in the famous question:
Python class inherits object
Please also refer to:
https://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html
Also, please note that there is a difference between them only in Python 2. In Python 3 you have no difference between these two types of declaration anymore (I know that your question is about Python 2, just a small note).
This question already has answers here:
Why can't you add attributes to object in python? [duplicate]
(2 answers)
Can't set attributes on instance of "object" class
(7 answers)
Closed 9 years ago.
I just realized that:
class A(object): pass
a = A()
a.x = 'whatever'
Works (does not raise an error and creates a new x member).
But this:
a = object()
a.x = 'whatever'
Raises:
AttributeError: 'object' object has no attribute 'x'
While I probably would never use this in real production code, I'm a bit curious about what the reason is for the different behaviors.
Any hints ?
Probably because of __slots__. By default your class have dict of all atributes which can be added to like in your first example. But that behaviour can bi overriden by using slots.
Also, some classes like datetime which are implemented in C also can not be extended with new attributes at runtime.
Workaround for such classes is to do something like :
class MyObject(): # extend that class, here we extend object
pass # add nothing to the class
o = MyObject()
o.x = 'whatever' # works
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
python class inherits object
In Python 2.7, what is the difference between:
class MyClass(Object):
and
class MyClass:
What does the Object do?
The Object in this case the base class of MyClass, meaning that it 'inherits' the methods and variables of Object unless overwritten. Inheriting from object, however, creates a 'new-style class' as opposed to an 'old-style class'. For more information, see jozzas' comment
See this tutorial for information about inheritance.