what is diff between both of class definition [duplicate] - python

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).

Related

Why are instances of old style classes instances of `object`? [duplicate]

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.

Why does defining a class in Python 2.7, without inheriting an object, not result in a __mro__ method? [duplicate]

This question already has answers here:
What is the difference between old style and new style classes in Python?
(8 answers)
Closed 7 years ago.
I am working on Mac OS X v10.10 (Yosemite) with Python 2.7.9.
Here is what I have tried:
Define a class
class A:
def test(self):
print "test"
Then run
A.__mro__
Then I got
>>> A.__mro__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__mro__'
Then I define
class B(object):
def test(self):
print "test"
Then run
B.__mro__
Then I got
>>> B.__mro__
(<class '__main__.B'>, <type 'object'>)
What is the different between the two definitions?
I found that in Python 3, the edition without "object" still has the __mro__ method.
__mro__ is only defined for new-style classes. In Python 2, a class is only new-style if it inherits from object (or from a built in type, which in turn inherits from object), while all classes in Python 3 are new-style no matter what.
The __mro__ is only defined for new style classes, those that inherit from Python object.
In Python 3, all classes are new-style classes. They inherit from object implicitly. In other words, old-style Python 2 classes have been removed from the language.

How super() implements in python programming [duplicate]

This question already has answers here:
How does Python's super() work with multiple inheritance?
(18 answers)
Closed 9 years ago.
I am new to python programming,
below is the example of parent,child classes,There are two direct super classes (i.e. bases) of C: A and B. A comes before B, so one would naturally think that the super class of C is A. However, A inherits its attribute a from T with value a=0: if super(C,c) was returning the superclass of C, then super(C,c).a would return 0 but its won't?
Could you please help me to understand why its returning 2.why not 0
>>> class T(object):
... a = 0
>>> class A(T):
... pass
>>> class B(T):
... a = 2
>>> class C(A,B):
... pass
>>> c = C()
>>>super(C,c).a
2
Thanks,
Hema
It has to do with Python method resolution order, or MRO. The precise definition of Python's MRO is here: Python 2.3's MRO doc. Edit: this explanation by Guido seems easier to read, and it includes an example exactly like yours.
If you call __mro__ on a class, you'll see the order in which it looks up stuff. In this case:
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.T'>, <type 'object'>)
So there you can see that it goes to B first, and only then to T. The reason is that it would otherwise be impossible to override attributes from T in B in this structure, and that's unlikely to be the desired behaviour.
Personally I just always avoid this diamond-style inheritance pattern, except of course for object which is the base class of every other class.

What does (object) do next to class name in python? [duplicate]

This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 6 years ago.
When you declare a class in python, I often see (object) written next to the class name.
class someClass(object):
def __init__(self, some_variable):
...
...
Is this same as writing below?
class someClass: # didn't write (object) here.
def __init__(self, some_variable):
...
...
I don't really see any difference in terms of how they function. Is it just a way to clarify that someClass is a subclass of object? and is it a good practice to explicitly write object when I make a class?
In Python 2, making someClass a subclass of object turns someClass into a "new-style class," whereas without (object) it's just a "classic class." See the docs or another question here for information on the differences between them; the short answer is that you should always use new-style classes for the benefits they bring.
In Python 3, all classes are "new-style," and writing (object) is redundant.
In python 3.x, they are the same, when you declare:
class C:
def __init__(self):
...
it inherits from object implicitly.
For more information visit this.

What does the object in python class header do? [duplicate]

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.

Categories

Resources