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/
Related
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I have to specify my own class when using super(), and is there a way to get around it?
I was reading this book - "Core Python Programming", and I really found it very nice I would say. But I got confused at a point while I was studying the topic Inheritance..
The book somewhere said that- we can use super() to invoke super class method which will find the base class method for us and also we don't need to pass self explicitly, like we do without super..
Here's the sample code: -
# Invoking super class method without super() .. Need to pass `self` as argument
class Child(Parent):
def foo(self):
Parent.foo(self)
# Invoking with super().
# No need to pass `self` as argument to foo()
class Child(Parent1, Parent2):
def foo(self):
super(Child, self).foo()
print 'Hi, I am Child-foo()'
My Question is - why we have to pass the classname to super() call.. As the classname can be inferred from the class from which super is invoked..
So, since super() is invoked from
class Child, classname should be implicit there.. So why do we need that??
*EDIT: - Giving Child as a parameter to super() doesn't make sense, because it doesn't give any information.. We could have used super(self).. And the method would have been searched in the super classes in the order they are given inside parenthesis..
By providing the class name as first argument you provide redundant information. Yes. That's a bit stupid* and that's why the behavior of super is changed in Python 3 to:
super().__init__()
*With my answer I actually contradict the answer by John Carter. Only one of us is right, of course. I don't want to offend him and others and I am happy to see a meaningful example where super(C, self).method(arg) is not actually used within class C :-).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between #staticmethod and #classmethod in Python?
I am learning OOP in python and came to know about these two methods
It seems that the difference in terms of syntax is that class methods are implicitly passed the class they belong to as their first parameter
class Circle:
all_circles = [] # class variable
#staticmethod
def total_area():
for c in Circle.all_circles: # hardcode class name
# do somethig
#classmethod
def total_area(cls):
for c in cls.all_circles: # no hardcode class name
# do something
I see class method as more flexible since we don't hardcode the class
Question:
- Is it even a question which one is better? #staticmethod or #classmethod?
- what are the scenarios suitable to use of each one of these methods?
A classmethod gets passed the class 'cls' that it was called upon. For more details see: What is the difference between #staticmethod and #classmethod in 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.