Difference in Python class declaration [duplicate] - python

This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 5 years ago.
In python class declaration I can declare a class by few ways. What is a difference between following samples?
class MyClass:
def __init__(self)
pass
class MyClass(object):
def __init__(self)
pass

The second declaration creates a new-style class. A new-style class is derived from a built-in type, in this case an object. This was introduced in python 2.2 in an effort to unify classes and types. For backward compatibility old-style classes are still the default
Additional read: http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html

The second way creates a "new-style" class. Documentation is admittedly a bit lacking, as mentioned in a couple places on the python website Python Guide 3.3, and here. There's also an essay describing their design by Python's creator (Guido van Rossum), but it's not strictly documentation.

Related

What are Arguments in Python Class Inheritance used for? [duplicate]

This question already has answers here:
What are metaclasses in Python?
(25 answers)
Closed 3 years ago.
When browsing the source code for the django_filters library, I found a class declaration syntax that I've never seen before: inheritance with arguments. I could not find an explanation in the official python class tutorial.
Here is the source, snippet below:
class FilterSet(BaseFilterSet, metaclass=FilterSetMetaclass):
pass
what does metaclass=FilterSetMetaclass in the class definition do?
There are two uses for keyword arguments in the list of base classes.
The metaclass argument is used specially to indicate which metaclass (instead of type) to use to create the class. (In Python 2, this was done by assigning a value to the class attribute __metaclass__ in the body of the class statement.)
A class statement is essentially a call to the metaclass.
class Foo(metaclass=Bar):
pass
is equivalent to Foo = Bar('Foo', (), {}). The default metaclass is type, that is
class Foo():
pass
is equivalent to
class Foo(metaclass=type):
pass
Other keyword arguments are passed along to __init_subclass__.
Both of the above are simplifications; see Customizing Class creation for more details.

Class definition parameter vs. Normal class definition [duplicate]

This question already has answers here:
What is the difference between old style and new style classes in Python?
(8 answers)
Closed 8 years ago.
What is the difference between the two? Don't both of these have the same functionality? I don't understand the whole point of the object parameter.
class Car(object): # object parameter
def foobar():
print("Hello World!\n")
vs.
class Car(): # No parameter
def foobar():
print("Hello World!\n")
In Python 2, the former is a "new-style class" and the latter is an "old-style class" that only exists for backwards compatibility. You should never use the latter for anything new.
In Python 3, I believe there is no difference at all. You can even leave out the parentheses entirely.
In two words:
# new style
class Car(object):
pass
A "New Class" is the recommended way to create a class in modern
Python.
# classic style
class Car():
pass
A "Classic Class" or "old-style class" is a class as it existed in
Python 2.1 and before. They have been retained for backwards
compatibility. This page attempts to list the differences.
Please look at:
http://python-history.blogspot.com/2010/06/new-style-classes.html
https://wiki.python.org/moin/NewClassVsClassicClass
https://www.python.org/doc/newstyle/

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.

In Python, what is the difference between "class name(object):" and "class name():"

What is the difference between the two classes below? Do you have some related information about this case? Thank you very much.
class test(object):
def __init__(self, name):
print name
class test():
def __init__(self, name):
print name
In python 2.x, the class that inherits from object will be a new-style class, while the other won't, while in python 3.x there'll be both new-style.
However, the differences between new and old are rather advanced, (for example, attribute search order) so a beginner shouldn't be too concerned about the incompatibilities.
See this answer for more information if you're interested, but it's rather a thing for library developers etc.
Mhmm ... this wiki-page explains the differences very illustratively: http://wiki.python.org/moin/NewClassVsClassicClass
And I saw some answeres with the information, that Old-(Classic)-Style and New-Style classes are the same in py3 -> that's not correct:
Old-style classes are removed in Python 3, leaving only the semantics of new-style classes
Besides this, the New-Style classes are quite available since Python 2.2. Up to 2.1 we have had to use the Classic style -> see here
Short summary about the differences/infos could be:
New-Style classes are available since Python 2.2
New-Style classes can use descriptors - Old Style classes cannot
New Style classes can subclass most built-in types - Old Style classes cannot
New Style classes supports a new meta-model (which affects e.g. the behaviour of the type() built-in massively)
Old-Style classes will find an attribute on an instance before it looks in the hierarchy - New-Style classes will let the class definition win if it is a writeable descriptor
Old-Style classes has been removed in Python 3
But in most way the introduction of the New-style classes has been affected within the comming up of python's Descriptors --> read more here.
In this case, none, because the first explicitly inherits from object as a base class, while the second inherits from object implicitly.

Categories

Resources