[Tutor] inheriting __init__ arguments
Don Arnold
Don Arnold" <darnold02@sprynet.com
Sat Apr 5 08:28:02 2003
----- Original Message -----
From: "david" <din22@cox.net>
To: <tutor@python.org>
Sent: Saturday, April 05, 2003 3:54 AM
Subject: [Tutor] inheriting __init__ arguments
hello everyone.
if i make a class like this
class A:
def __init__(self,name):
self.name=name
then i make another class that inherits from A its name attribute
class B(A):
def __init__(self, address):
A.__init__(self,name)
self.address=address
but instances of B have addresses also.
how can B inherit its name from initializing A?
if my question makes any sense , please explain?
[my reply:]
Your question makes perfect sense. You just need to pass name as an argument
to B( ) so it can forward it on to A's __init__:
class A:
def __init__(self,name):
self.name=name
class B(A):
def __init__(self, name, address): ## added name parameter
A.__init__(self,name)
self.address=address
me = B('Don','12345 Main St')
print me.name
print me.address
>>> Don
>>> 12345 Main St
HTH,
Don