0

So I tried to write an terminal based RPG in order to learn OOP, so I wrote this code:

class car(object):  #car is the short form of Character
  def __init__(self):
    self.leben = 100
    self.speed = 10
    self.SD = 20
  def attack(self, life):
    #life-= self.SD
    #return life
    pass
  def stats(self):
    print 'Leben: '
    print self.leben
    print 'Geschwindigkeit: ' 
    print self.speed
    print 'Schaden: '
    print self.SD

class wojok(car): 
  def __init__(self):
    super(car.__init__(self)).__init__()
    self.SD = 50


C = wojok()
while True:
  e = raw_input('Befehl: ')
  if e == 'info':
    C.stats()
  elif e == 'stop':
    break

Now I get the error:

TypeError: unbound method __init__() must be called with car instance as first argument(got nothing instead)

But when I try to pass an instance of car as the first argument into init I get the error:

TypeError: unbound method __init__() must be called with car instance as first argument(got instance instead)

What do I have to use as first argument?

vusan
  • 5,221
  • 4
  • 46
  • 81
Gilgamesch
  • 313
  • 2
  • 10
  • 24
  • Possible duplicate of [unbound method must be called with instance as first argument (got nothing instead)](http://stackoverflow.com/questions/19442466/unbound-method-must-be-called-with-instance-as-first-argument-got-nothing-inste) – gmauch Jun 09 '16 at 17:27
  • @gmauch I don't think that's it, especially looking at the accepted answer. The problem seems to be the `super` being misused in OP's code (so trying to call an unbound `__init__`). – Andras Deak -- Слава Україні Jun 09 '16 at 17:30
  • @AndrasDeak Im new to Python and might have missed some details,but I have the same problem and Google shows 7 questions with almost the same title([1](http://stackoverflow.com/q/32010905/4385621),[2](http://stackoverflow.com/q/4473184/4385621),[3](http://stackoverflow.com/q/24833338/4385621),[4](http://stackoverflow.com/q/26386644/4385621),[5](http://stackoverflow.com/q/19442466/4385621),[6](http://stackoverflow.com/q/10645444/4385621) and [7](http://stackoverflow.com/q/17437837/4385621)).It'll take me some time to analyze each and flag them accordingly,unless theres some quicker way to do it – gmauch Jun 09 '16 at 17:59
  • @gmauch well in this specific case, this question has a good and specific answer, while your dupe target had a pretty vague explanation. In any case, I'd advise against flagging all sorts of old and inactive questions for closure. That just doesn't help the community:) If you wish to flag for closure, aim new questions, that way you can *prevent* new answers to duplicate questions. The general idea is "only flag/vote to close old questions if there has been some very new activity on them". – Andras Deak -- Слава Україні Jun 09 '16 at 20:31
  • @AndrasDeak Thanks for the explanation, but where do I found this "only flag/vote to close old questions if there has been some very new activity on them" rule? Seems to me that a dupe is a dupe no matter how old. In my case (and anybody else with the same problem) it would be better if there was only one question referenced, so I wouldn't have to search in 7 (supposedly) very similar questions. Looks like the OPs in each one of them didn't do much of a search here in StackOverflow before asking. Do you think is it worth to ask that in Meta? – gmauch Jun 09 '16 at 20:53
  • @gmauch it's partly experience with the close vote review queue, partly informal culture in the [SOCVR chat room](http://chat.stackoverflow.com/rooms/info/41570/so-close-vote-reviewers). But you're right that duplicates are a bit different, I usually have off-topic and other low-quality posts in mind (there are a lot of very old very off-topic questions around). Anyway, it's your vote, so you can still try (the worst that can happen that the flag will age away). Although I have to say, that even if all these questions pointed to a single duplicate, askers would *still* not bother to search:) – Andras Deak -- Слава Україні Jun 09 '16 at 21:05

1 Answers1

4

in

class wojok(car): 
  def __init__(self):

this line:

    super(car.__init__(self)).__init__()

should be

    super(wojok, self).__init__() 

But if all you want is to have different car instances with different attributes values, just pass them to the initializer (possibly with default values) :

class Character(object):
    def __init__(self, leben=100, speed=10, sd=20):
        self.leben = leben
        self.speed = speed
        self.sd = sd

Then you either instantiate Character with no arguments to get the default values or specify what you want, i.e.:

default_char = Character()
special_char = Character(sd=100)

NB : python naming conventions: use CapCase for classes, all_lower for variables and functions, and ALL_UPPER for pseudo-constants, and prefer descriptive names to abbreviations (unless the abbreviation is really a 'descriptive' name by itself).

Of course if a wojok instance is supposed to have a different behaviour from a "standard" Character (and you omitted it because it's not relevant here) subclassing is an appropriate solution ;)

alextansc
  • 4,626
  • 6
  • 29
  • 45
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118