class
class inherit from object
class MyClass(object):
# class code follows...Python 2, many reasons.
Python 3, no reason.
The following 2 classes are complete the same.
class ClassicSpam:
pass
class NewSpam(object):
passABC, Abstract Base Classes
http://www.thedigitalcatonline.com/blog/2016/04/03/abstract-base-classes-in-python/
[1] to resolve: no-type-checking in python
try:
someobj[1]
except TypeError:
# when object is not subscriptable[2] define sync interface among parent and child class.
Define it
by real inheritance
by virtual subclass
Registering a class does not imply any form of check about methods or attributes. Registering is just the promise that a given behaviour is provided by the registered class.
Create ABC
Metaclass
metaclass = mixin
standard case:
calling get_name() on c, and it will check Child -> Parent -> GrandParent -> object
metaclass case:
calling get_name() on c, and it will check Child -> Parent -> GrandParent -> object -> NewType
abstractmethod = class with interface
prevents the class from being instantiated if the method is not implemented
Last updated
Was this helpful?