TAGS :Viewed: 7 - Published at: a few seconds ago

[ Unbound Method With Class Instance ]

I'm trying to figure out how to correct my error because I"m not quite sure I understand what is going on with the error.

File "/Users/me/PycharmProjects/Project/project/main.py", line 15, in get
    do = Player.pitcher()
TypeError: unbound method pitcher() must be called with Player instance as first argument (got nothing instead)

-

import webapp2
from pages import Page, ContentPage
from data import Player

class MainHandler(webapp2.RequestHandler):
    def get(self):
        page = Page()
        content = ContentPage()
        if self.request.GET:
            id = self.request.GET['id']
            type = self.request.GET['type']
            name = self.request.GET['name']
            if type == 'pitcher':
                pitcher_data = [['1', '22', '2.41', '48', '24'], ['2', '16', '1.98', '61', '0'], ['3', '11', '5.18', '22', '0']]
                do = Player.pitcher()
                do.ip = ''....

        self.response.write(page.print_out())

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)


class Player(object):
    def __init__(self):
        self.id = ''
        self.player_name = ''
        self.position = ''

    def fielder(self):
        self.bats = ''
        self.throws = ''
        self.avg = ''
        self.hr = ''
        self.rbi = ''

    def pitcher(self):
        self.ip = ''
        self.era = ''
        self.strikeouts = ''
        self.walks = ''
        self.sv = ''

Answer 1


You're invoking pitcher on a class object Player and not an instance of the class.

In Python an instantiated class (object) contains instance data in the form of a dictionary __dict__ that is passed as the first argument self to a method when it's invoked on the object.

To use your code as-is the correct call would be do = Player().pitcher()

This is an example of a chained call... Player() creates and returns an instance of the Player object which contains the instance data dictionary and .pitcher() invokes the pitcher method on the newly created instance.

EDIT: Looking at your example code again it appears that what you're probably trying to do is create two different objects which inherit from some common state. This can be done using subclasses.

For example:

class Player(object):
    def __init__(self):
        self.id = ''
        self.player_name = ''
        self.position = ''

class Fielder(Player):
    def __init__(self):
        super(Fielder, self).__init__()  # Call the constructor for the Player base class
        self.bats = ''
        self.throws = ''
        self.avg = ''
        self.hr = ''
        self.rbi = ''

class Pitcher(Player):
    def __init__(self):
        super(Pitcher, self).__init__()  # Call the constructor for the Player base class
        self.ip = ''
        self.era = ''
        self.strikeouts =''
        self.walks = ''
        self.sv = ''

Now in your main code you'd do something like

if type == 'pitcher':
    player = Pitcher()
    player.ip = <some value>
    ...
elif type == 'fielder':
    player = Fielder()
    player.bats = <some value>
... etc ...

Answer 2


add @staticmethod to pitcher function or create a Player instance before calling its functions