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.
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:
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/
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.
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.