A
Alex Hall
Hi all,
I am now trying to allow my classes, all of which subclass a single
class (if that is the term), to provide optional arguments. Here is
some of my code:
class Craft():
def __init__(self,
name,
isAircraft=False,
id=helpers.id(),
hits=0,
weapons=[]):
self.name=name
self.id=id
self.hits=hits
self.weapons=weapons
self.isAircraft=isAircraft
#end def
#end class
#now a class for each type of craft in the game
#each will subclass the Craft class, but each also has its own settings
class Battleship(Craft):
def __init__(self,
name,
maxHits):
Craft.__init__(self, name)
self.maxHits=maxHits
self.length=maxHits #in Battleship, each ship's length is the same
as how many times it can be hit
#end def
#end class
I want to be able to say something like
b=Battleship("battleship1", 4, weapons=["missile1","missile2"])
When I do that, though, I get a traceback on the above line saying
"type error: __init__() got an unexpected keyword argument 'weapons'".
What did I do wrong / what do I need to change so that any Craft
(Battleship, Carrier, and so on) can optionally provide a list of
weapons, or any other arguments to which I assign default values in my
Craft class? I hope this makes sense.
I am now trying to allow my classes, all of which subclass a single
class (if that is the term), to provide optional arguments. Here is
some of my code:
class Craft():
def __init__(self,
name,
isAircraft=False,
id=helpers.id(),
hits=0,
weapons=[]):
self.name=name
self.id=id
self.hits=hits
self.weapons=weapons
self.isAircraft=isAircraft
#end def
#end class
#now a class for each type of craft in the game
#each will subclass the Craft class, but each also has its own settings
class Battleship(Craft):
def __init__(self,
name,
maxHits):
Craft.__init__(self, name)
self.maxHits=maxHits
self.length=maxHits #in Battleship, each ship's length is the same
as how many times it can be hit
#end def
#end class
I want to be able to say something like
b=Battleship("battleship1", 4, weapons=["missile1","missile2"])
When I do that, though, I get a traceback on the above line saying
"type error: __init__() got an unexpected keyword argument 'weapons'".
What did I do wrong / what do I need to change so that any Craft
(Battleship, Carrier, and so on) can optionally provide a list of
weapons, or any other arguments to which I assign default values in my
Craft class? I hope this makes sense.