If I make a derived class, will it automatically have all the attributes of the base class?
Class attributes, yes. Instance attributes, no (simply because they don't exist when the class is created), unless there's no __init__ in the derived class, in which case the base one will be called instead, and will set the instance attributes.
Does BaseClass.init() need arguments?
Depends on the class and its __init__ signature. If you're explicitly calling Base.__init__ in the derived class, you will at least need to pass self as the first argument. If you have
class Base(object):
def __init__(self):
# something
then it's rather obvious that no other arguments are accepted by the __init__. If you'd have
class Base(object):
def __init__(self, argument):
# something
then you have to pass argument when calling base __init__. No rocket science in here.
If you have arguments for your base class init(), are they also used by the derived class, do you need to explicitly set the arguments to the derived classe's init(), or set them to BaseClass.init() instead?
Again, if the derived class doesn't have __init__, base one will be used instead.
class Base(object):
def __init__(self, foo):
print 'Base'
class Derived(Base):
pass
Derived() # TypeError
Derived(42) # prints Base
In other case, you need to take care of it somehow. Whether you use *args, **kwargs and just pass arguments unmodified to the base class, or copy the base class signature, or supply arguments from elsewhere, depends on what you're trying to accomplish.
answered 2011-06-18T14:02:16.137