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

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.

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.

what is diff between both of class definition [duplicate]

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

Why can't I add arbitrary members to object instances? [duplicate]

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

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.

Difference in Python class declaration [duplicate]

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.

Categories

Resources