ugeb
asked on
python subclass with extra arguments?
Hi,
I'm trying to figure out Python's subclassing when the subclass requires additional arguments. I've put together a very simple program just to try things out, but I can't seem to get the syntax Python likes.
nt file
I need to instantiate the Abcd class, which should call the super's init and use the extra arguments for additional processing. However, no matter how many arguments I add or remove, it always says I'm giving too many or too few.
What's the syntax if I want the Abcd class to have an argument 'd' in addition to a,b and c and call the super's init with a, b and c?
Thanks!
I'm trying to figure out Python's subclassing when the subclass requires additional arguments. I've put together a very simple program just to try things out, but I can't seem to get the syntax Python likes.
class Abc(object):
def __init__(self,a,b,c=None):
self.a = a
self.b = b
self.c = c
def pr(self):
print "{}, {}, {}".format(self.a,self.b,self.c)
#------------------Differeimport abc_class as ac
class Abcd(ac.Abc):
def __init__(self, a,b,c,d): #What goes here for a, b, c and d?
# Tried everything here. What's this super call supposed to look?
super( Abcd, self,a).__init__()like?
if __name__ == '__main__':
x = Abcd(4,5,6)
x.pr()
I subclassed Abc from object because I read something about old vs. new style classes and the new style require the base class object (I'm obviously no expert ...)I need to instantiate the Abcd class, which should call the super's init and use the extra arguments for additional processing. However, no matter how many arguments I add or remove, it always says I'm giving too many or too few.
What's the syntax if I want the Abcd class to have an argument 'd' in addition to a,b and c and call the super's init with a, b and c?
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree, but why would I use object explicitly? There should be a specific reason for making things more complicated.
a few reasons (though in many cases they don't really matter, but well . . ):
- in order to avoid any subtle differences in behaviour which one might forget / overlook.
( special methods / descriptors / method resolution order in case of multiple inheritance )
- Python 3 will only have the behaviour of old style classes
- decorators like @classmethod and @staticmethod
- just out of habit ;-)
- for consistency reasons
What can also be annoying is if you want to inherit from an old style class and suddenly some features don't work (descriptors) in derived classes.
At least in our team (using Python 2.7) we don't use any old style classes for our own code
as is the official recommendation.
- in order to avoid any subtle differences in behaviour which one might forget / overlook.
( special methods / descriptors / method resolution order in case of multiple inheritance )
- Python 3 will only have the behaviour of old style classes
- decorators like @classmethod and @staticmethod
- just out of habit ;-)
- for consistency reasons
What can also be annoying is if you want to inherit from an old style class and suddenly some features don't work (descriptors) in derived classes.
At least in our team (using Python 2.7) we don't use any old style classes for our own code
as is the official recommendation.
ASKER
Yes indeed it is Python 2.7 and your solution works well. Thanks to both!
why would I use object explicitly?
- Python 3 will only have the behaviour of old style classes
The official documentation says for Python 3 (http://docs.python.org/3/reference/compound_stmts.html#class-definitions)
Classes without an inheritance list inherit, by default, from the base class object; hence,
class Foo:
pass
is equivalent to
class Foo(object):
pass
- Python 3 will only have the behaviour of old style classes
was a typo.
I wanted to write" new style" of course.
Thus in order to have most similiar behaviour for code potentially running on 2.7 and 3.0 I always inherit from object .
Whatever. As I said in my previous post. in most real life cases it won't make a difference.
I'm just being very conservative.
you can also use new style classes (so Base inherits from object)
and not use super()
so:
Open in new window