how to pass keyword arguments to derived class~

B

black

Hi all~

I met problem when trying passing keyword arguments to derived class. my goal is to pass all keyword arguments to taht derived class as they did for superclass, but the result was not exactly what i want. below is my code:

class A:
def __init__(self, *args, **kw):
print args
print kw
print
class B(A):
def __init__(self, *args, **kw):
A.__init__(self, args, kw=kw)
a = A("a?", a_pro="a!")
b = B("b?", b_pro="b!")


here is the output:

('a?', )
{'a_pro':'a!'}

(('b?', ), ) # I want to get simply ('b?', ) here
{'kw':{'b_pro':'b!'}} # Here should also be ('b_pro':'b!')

any help ???
 
J

JZ

class A:
def __init__(self, *args, **kw):
print args
print kw
print
class B(A):
def __init__(self, *args, **kw):
A.__init__(self, args, kw=kw)
a = A("a?", a_pro="a!")
b = B("b?", b_pro="b!")


here is the output:

('a?', )
{'a_pro':'a!'}

(('b?', ), ) # I want to get simply ('b?', ) here
{'kw':{'b_pro':'b!'}} # Here should also be ('b_pro':'b!')

any help ???

I used Python 2.3.3. What is your version?
def __init__(self, *args, **kw):
print args
print kw
print
def __init(self, *args, **kw):
A.__init__(selg,arg,kw)

('a?',)
{'apro': 'a!'}
('b?',)
{'bpro': 'b!'}
 
J

JZ

class A:
def __init__(self, *args, **kw):
print args
print kw
print
class B(A):
def __init__(self, *args, **kw):
A.__init__(self, args, kw=kw)
a = A("a?", a_pro="a!")
b = B("b?", b_pro="b!")


here is the output:

('a?', )
{'a_pro':'a!'}

(('b?', ), ) # I want to get simply ('b?', ) here
{'kw':{'b_pro':'b!'}} # Here should also be ('b_pro':'b!')

any help ???

I used Python 2.3.3. What is your version?
def __init__(self, *args, **kw):
print args
print kw
print
def __init(self, *args, **kw):
A.__init__(self,arg,kw)

('a?',)
{'apro': 'a!'}
('b?',)
{'bpro': 'b!'}
 
S

sdd

black said:
> ...
class A:
def __init__(self, *args, **kw):
print args
print kw
print
class B(A):
def __init__(self, *args, **kw):
A.__init__(self, args, kw=kw)
class C(A):
def __init__(self, *args, **kw):
A.__init__(self, *args, **kw)
a = A("a?", a_pro="a!")
b = B("b?", b_pro="b!")
c = C("c?", c_pro="c!")

Suggestion:
Really use the standard python convention of 4 characters per indent.
We promise not to charge you for extra spaces. This code was a trifle
harder to cut out of the message and paste into idle to replicate. It
also does help in reading code to trust there are no subtle indents.

-Scott David Daniels
(e-mail address removed)
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top