Here's some code I wrote with the purpose in mind of having more than one way to initialize my GameObject class:
class GameObject(object):
def __init__(self, name, location, description, aliases=[]):
self.name = name
self.location = location
self.description = description
self.aliases = aliases
...
@classmethod
def from_values(cls, name, location, description, aliases=[]):
return cls(name, location, description, aliases)
@classmethod
def from_super(cls, object):
return cls(object.name, object.location, \
object.description, object.aliases)
Specifically, the way I'm trying to use this is to call my from_super()
method from the children classes of GameObject and return an instance of that child class, GIVEN a GameObject instance. This looks like:
item = o.GameObject(name, room.id, description, aliases)
if item_type == 'fix':
item = fix.Fixture.from_super(item)
if item_type == 'move':
item = move.Moveable.from_super(item)
if item_type == 'take':
item = take.Takeable.from_super(item)
# do something with item
And in Takeable:
import object
class Takeable(object.GameObject):
def __init__(self, name, current_room, description):
object.GameObject.__init__(self, name, current_room, description)
...
I thought this rather nifty, but unfortunately when I execute that last bit, it fails inside the code for GameObject, and with a very familiar error...
Traceback (most recent call last):
File "create_map.py", line 207, in
create_map()
File "create_map.py", line 191, in create_map
create_items(room)
File "create_map.py", line 83, in create_items
item = take.Takeable.from_super(item)
File "/home/brian/Code/chimai/chimai/objects/object.py", line 21, in from_super
return cls(object.name, object.location, object.description, object.aliases)
TypeError:__init__()
takes exactly 4 arguments (5 given)
... which I don't understand. Typically when I get this error, I forgot to include self. However, I did not forget in this case, as you can see. So I'm thinking the error has to do with something going on behind the scenes with my class method, and I was just starting to try to research exactly what that magic is, but I thought I might have one of you kind people help me to understand instead.
Thanks in advance, and sorry if I missed something obvious.