initializing cooperative method

J

jelle

Hi,

I'm writing a bunch of abstract classes and I'd like to delegate the
arguments of the concrete class the to abstract one. I was surprised
to see that the print statement in the abstract class isn't executed.
But moreover, I'd like to find out an idiom that allows one to
delegate arguments in the inherited class. Any ideas?

Thanks,

-jelle



class Abstract(object):
def __init__(self, a='a', b='b'):
self.a, self.b = a, b
print a, b

class Concrete(Abstract):
def __init__(self, a='AAA', b='BBB'):
super(Abstract, self).__init__( a=a, b=b )
print a, b

c = Concrete(a=1) 1 BBB
 
S

Steven D'Aprano

Hi,

I'm writing a bunch of abstract classes and I'd like to delegate the
arguments of the concrete class the to abstract one. I was surprised to
see that the print statement in the abstract class isn't executed. But
moreover, I'd like to find out an idiom that allows one to delegate
arguments in the inherited class. Any ideas?

Change the call to super() in Concrete from:

super(Abstract, self).__init__(a, b)

to

super(Concrete, self).__init__(a, b)
 
J

jelle

Ai, calling super(Abstract) is just getting object, sure...

However, I'm still curious to know if there's a good idiom for
repeating the argument in __init__ for the super(Concrete,
self).__init__ ?

Thanks,

-jelle
 
J

jelle

Ai, calling super(Abstract) is just getting object, sure...

However, I'm still curious to know if there's a good idiom for
repeating the argument in __init__ for the super(Concrete,
self).__init__ ?

Thanks,

-jelle
 
J

Jonathan Gardner

Ai, calling super(Abstract) is just getting object, sure...

However, I'm still curious to know if there's a good idiom for
repeating the argument in __init__ for the super(Concrete,
self).__init__ ?

If you don't know what the arguments are, you can use *args and
**kwargs.

class Concrete(Abstract):
def __init__(self, *args, **kwargs):
super(Concrete, self).__init__(*args, **kwargs)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top