3

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.

Brian Peterson
  • 2,800
  • 6
  • 29
  • 36
  • 1
    Could you post the whole/more of the traceback so it's easier to follow the flow? – timss Apr 21 '13 at 02:29
  • 4
    Be careful with mutable default arguments: http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument – Blender Apr 21 '13 at 02:34
  • Posted the traceback. – Brian Peterson Apr 21 '13 at 02:36
  • Do you have `__init__` in `Takeable`? – gatto Apr 21 '13 at 02:39
  • @Blender: I have visited that thread recently, lol. And I half-expect to run into some problems from carelessly passing around mutable/non-mutable objects soon. But is this related to the error? – Brian Peterson Apr 21 '13 at 02:39
  • Ohhhhhh. Thank you @gatto, I think I understand. `Takeable` doesn't have the `aliases` param. – Brian Peterson Apr 21 '13 at 02:43
  • 1
    Well, there you go ;) As for mutable default arguments, it's not related. – gatto Apr 21 '13 at 02:46
  • If someone posted an answer, I'd accept, lol. – Brian Peterson Apr 21 '13 at 02:47
  • 1
    Just a quick tip, it's probably best not to ever have a python file called "object.py". You're going to get very confused when you start trying to define classes that inherit from `object` in modules where you've done `import object`. – Ben Apr 21 '13 at 02:48
  • In object.py, I in fact already have code that goes like this: class GameObject(object): ... Yeah, I guess that is bad. Advice well-taken. – Brian Peterson Apr 21 '13 at 02:51
  • 1
    Inside object.py that works, because there `object` is the normal `object` from Python. In any module where you've done `import object`, that name will now refer to the module, so if you tried `class SomeClass(object)` you'll get weird errors because you're trying to create a class that inherits from a module. And then you'll look at your class statement and everything will look fine, and then you'll try typing it into a Python interpreter and it will work, and it'll take you ages to figure out where the problem really is. Save yourself while you still can! – Ben Apr 21 '13 at 03:13

0 Answers0