I was reading the answers to Usage of __slots__? and I noticed that one of the examples is :
from collections import namedtuple
class MyNT(namedtuple('MyNT', 'bar baz')):
"""MyNT is an immutable and lightweight object"""
__slots__ = ()
I saw that the __init__
of namedtuple
was called when it was being subclassed by MyNT
I went ahead and tested it for myself and made this code which is my first attempt to understand such behavior:
class objectP():
def __init__(self,name):
print('object P inited')
class p(objectP('asd')):
pass
I got an error stating that 4
objects were "passed" so I changed it to
class objectP():
def __init__(self,*a):
print('object P inited')
class p(objectP('asd')):
pass
which now produces an output of
object P inited
object P inited
- What does the line of code above mean? Calling
__init__
when subclassing? - Why is
object P inited
printed twice?