Class Object
x = MyClass()
creates a new instance of the class and assigns this object to the local variable x
.
Init
Inheritance
class DerivedClassName(modname.BaseClassName):
Python has two built-in functions that work with inheritance:
- Use
isinstance()
to check an instance’s type:isinstance(obj, int)
will beTrue
only ifobj.__class__
isint
or some class derived fromint
. - Use
issubclass()
to check class inheritance:issubclass(bool, int)
isTrue
sincebool
is a subclass ofint
. However,issubclass(float, int)
isFalse
sincefloat
is not a subclass ofint
.
Multiple Inheritance
class DerivedClassName(Base1, Base2, Base3):
For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy.
Thus, if an attribute is not found in DerivedClassName
, it is searched for in Base1
, then (recursively) in the base classes of Base1
, and if it was not found there, it was searched for in Base2
, and so on.
Private Variables
Any identifier of the form __spam
(at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam
, where classname
is the current class name with leading underscore(s) stripped.
This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.