-1

I have the following two classes set up:

class Player:

    POINTS_PER_PASSING_YARD = 0.04
    POINTS_PER_PASSING_TOUCHDOWN = 4
    POINTS_PER_INTERCEPTION = -1
    POINTS_PER_RUSHING_YARD = 0.1
    POINTS_PER_RUSHING_TOUCHDOWN = 6
    POINTS_PER_RUSHING_FUMBLE = -2
    POINTS_PER_RECEPTION_YARD = 0.1
    POINTS_PER_RECEPTION_TOUCHDOWN = 6

def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
    self.name = name
    self.team = team
    self.rushingYards = rushingYards
    self.rushingTouchdowns = rushingTouchdowns
    self.rushingFumbles= rushingFumbles

def calculatePoints(self):
    return self.rushingYards * POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * POINTS_PER_RUSHING_FUMBLE

def toString(self):
    return "name: " + self.name + " team: " + self.team + " passing yards: " + self.passingYards + " rushing yards: " + self.rushingYards + " touchdowns: " + self.touchdowns + " interceptions: " +     self.interceptions

Then I have a QB class that inherits from Player:

from Player import *

class QB(Player):
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
        super().__init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles)
        self.passingYards = passingYards
        self.passingTouchdowns = passingTouchdowns
        self.interceptions = interceptions

    def toString(self):
        return "position: " + self.position + super().toString()

Then in my main class, I simply do:

myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
print(myQB)

I'm getting the following error:

Traceback (most recent call last):
  File "main.py", line 35, in <module>
    main()
  File "main.py", line 32, in main
    myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
  File "/Users/benjaminclayman/Desktop/Aurora_Fantasy_Football/QB.py", line 5, in __init__
    Player.__init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles)
TypeError: object.__init__() takes no parameters

But I'm not sure why, since all of the init methods I've written do take parameters...

Any idea what's going wrong?

Thanks, bclayman

ely
  • 74,674
  • 34
  • 147
  • 228
anon_swe
  • 8,791
  • 24
  • 85
  • 145
  • Which version of python are you using? (2 or 3?) – NightShadeQueen Jul 19 '15 at 17:39
  • (because I can't reproduce in python 3.4.) – NightShadeQueen Jul 19 '15 at 17:40
  • @NightShadeQueen I was able to reproduce this - `TypeError: object.__init__() takes no parameters` , with the `__init__()` method missing from `Player` class. – Anand S Kumar Jul 19 '15 at 17:42
  • 1
    Something seems a bit strange. You are using `super` in the manner permitted for Python 3, which means that even though your `Player` class does not explicitly subclass `object`, it is still a new-style class and the use of `super` like this should just work. But the fact that `Player` is not explicitly a new-style class makes me wonder if you're somehow using this in Python 2? Can you clarify? – ely Jul 19 '15 at 17:43
  • Oh, point, I fixed the indentation when testing. I get `TypeError: __init__() takes 6 positional arguments but 7 were given` when the indents are fixed. – NightShadeQueen Jul 19 '15 at 17:43
  • In Python 2 you should get a different error, since `super` in Python 2 requires at least one argument. So it seems more likely that it is Python 3. – ely Jul 19 '15 at 17:45

3 Answers3

3

If your indentation in the first script (with the Player class) is correct, then that is the issue.

According to your indentation , the __init__() method and other methods are outside the class Player , so Player uses object class' __init__() , which does not take any parameters (other than self , though we do not need to pass it explicitly ).

You may want to fix the indentation so that all the instance methods (that you intended to be inside Player class) come inside the Player class.

Example -

class Player:

    POINTS_PER_PASSING_YARD = 0.04
    POINTS_PER_PASSING_TOUCHDOWN = 4
    POINTS_PER_INTERCEPTION = -1
    POINTS_PER_RUSHING_YARD = 0.1
    POINTS_PER_RUSHING_TOUCHDOWN = 6
    POINTS_PER_RUSHING_FUMBLE = -2
    POINTS_PER_RECEPTION_YARD = 0.1
    POINTS_PER_RECEPTION_TOUCHDOWN = 6

    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
        self.name = name
        self.team = team
        self.rushingYards = rushingYards
        self.rushingTouchdowns = rushingTouchdowns
        self.rushingFumbles= rushingFumbles

You will need to do this for all methods you intended to be inside Player class.


One more issue, you should not pass self in to te __init__() method called using super() .

Example -

class QB(Player):
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
        super().__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

Few issues, first you need to fix your indentation.

second, you should change

 this class Player:

TO

 class Player(object):

another issue is when you call a static member this is the way to handle them:

Player.POINTS_PER_RUSHING_YARD  # name of the class and then the static member.

This is how your super should look like:

super(QB, self).__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)

This is the code:

    class Player(object):
    POINTS_PER_PASSING_YARD = 0.04
    POINTS_PER_PASSING_TOUCHDOWN = 4
    POINTS_PER_INTERCEPTION = -1
    POINTS_PER_RUSHING_YARD = 0.1
    POINTS_PER_RUSHING_TOUCHDOWN = 6
    POINTS_PER_RUSHING_FUMBLE = -2
    POINTS_PER_RECEPTION_YARD = 0.1
    POINTS_PER_RECEPTION_TOUCHDOWN = 6
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
        self.name = name
        self.team = team
        self.rushingYards = rushingYards
        self.rushingTouchdowns = rushingTouchdowns
        self.rushingFumbles= rushingFumbles
    def calculatePoints(self):
        return self.rushingYards * Player.POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * Player.POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * Player.POINTS_PER_RUSHING_FUMBLE
    def toString(self):
        return "name: " + self.name + " team: " + self.team + " passing yards: " + self.passingYards + " rushing yards: " + self.rushingYards + " touchdowns: " + self.touchdowns + " interceptions: " +     self.interceptions


class QB(Player):
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
        super(QB, self).__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
        self.passingYards = passingYards
        self.passingTouchdowns = passingTouchdowns
        self.interceptions = interceptions

    def toString(self):
       return "position: " + self.position + super(Player).toString()


myQB = QB("Brees", "Saints", 0, 0, 0, 4952, 33, 17)
print(myQB)
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • Why is this necessary - `class Player(object):` ? By default the parent class is `object` , unless specified otherwise? – Anand S Kumar Jul 19 '15 at 17:45
  • And I am guessing the above code is Python 3.x , in which case `super()` is fine. – Anand S Kumar Jul 19 '15 at 17:46
  • @AnandSKumar , New style objects have a different object model to classic objects, and some things won't work properly with old style objects, for instance, super(), property and descriptors. - taken from here http://stackoverflow.com/questions/4015417/python-class-inherits-object – omri_saadon Jul 19 '15 at 17:49
  • @ omri_saadon Old style objects are no longer there in Python 3, (I am guessing user is using Python 3) , as otherwise OP would be getting a different error when trying to use `super()` . – Anand S Kumar Jul 19 '15 at 17:51
  • @AnandSKumar Didn't noticed for python 3.x, I'm using 2.x.. my mistake – omri_saadon Jul 19 '15 at 17:52
0

In Python the indentation plays the major role. List of Issues: 1) The __init__() should be within the class Player. Otherwise your initialization will not work.

2) The Other functions def calculatePoints(self), should also be within the class Player. Currently in your code it is defined outside the class.

3) The def toString(self) should also be within the class Player.

3.1) Also the concatenation of string and int is not permitted. 
3.2) The variable `self.touchdowns` is not initialized in the def toString of class Player.
3.3) The alternate way to print the class with significant information 
     about class can be written in this manner.

class Player:
    POINTS_PER_PASSING_YARD = 0.04
    POINTS_PER_PASSING_TOUCHDOWN = 4
    POINTS_PER_INTERCEPTION = -1
    POINTS_PER_RUSHING_YARD = 0.1
    POINTS_PER_RUSHING_TOUCHDOWN = 6
    POINTS_PER_RUSHING_FUMBLE = -2
    POINTS_PER_RECEPTION_YARD = 0.1
    POINTS_PER_RECEPTION_TOUCHDOWN = 6

    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles):
        self.name = name
        self.team = team
        self.rushingYards = rushingYards
        self.rushingTouchdowns = rushingTouchdowns
        self.rushingFumbles= rushingFumbles

    def calculatePoints(self):
        return self.rushingYards * POINTS_PER_RUSHING_YARD + self.rushingTouchdowns * POINTS_PER_RUSHING_TOUCHDOWN + self.rushingFumbles * POINTS_PER_RUSHING_FUMBLE

    def __str__(self):
        return "name: %s team: %s passing yards: %r rushing yards: %r rushingTouchdowns: %r interceptions: %r"  % (self.name, self.team, self.passingYards, self.rushingYards, self.rushingTouchdowns ,self.interceptions)

4) self.position = position need to be initialized in the class QB, like this:

class QB(Player):
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, passingYards, passingTouchdowns, interceptions, position="QB"):
        super().__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles)
        self.passingYards = passingYards
        self.passingTouchdowns = passingTouchdowns
        self.interceptions = interceptions
        self.position = position

    def __str__(self):
        return "position: " + self.position + super().__str__()
kvivek
  • 3,321
  • 1
  • 15
  • 17