1

I am just writing some dummy code for pygame.

The first sample of code has a function in the menus.py file. I wanted to practice using import. This works fine. I then wanted to put the function in a class so I can get up and running with classes. This is the second block of code. Unfortunately the second block of code doesn't run. Could someone explain where I am going wrong please.

# menus.py
def color_switcher(counter, screen):
    black = ( 0, 0, 0)
    white = (255, 255, 255)
    green = (0, 255, 0)
    red = (255, 0, 0)

    colors = [black, white, green, red]
    screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.color_switcher(counter, screen)
     #more stuff

This works fine.

This doesn't

# menus.py
class Menu:

    def color_switcher(self, counter, screen):
        black = ( 0, 0, 0)
        white = (255, 255, 255)
        green = (0, 255, 0)
        red = (255, 0, 0)

        colors = [black, white, green, red]
        screen.fill(colors[counter])

# game.py

#stuff
if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu.color_switcher(counter, screen)
     #more stuff

#TypeError: unbound method color_switcher() must be called with Menu instance as first argument (got int instance instead)

Could someone tell me what I am doing wrong with the class please?

4
  • Note: if you're using Python 2, you should use class Menu(object): rather than class Menu:. Commented Sep 22, 2011 at 13:57
  • 2
    Things to read about which will help you: staticmethod, classmethod and object instances in general. Also note that in Python if you're not having any instance data---if all your "methods" in the class are just static methods, not needing to use self---you generally shouldn't be using classes. Use functions in a module. They're not bad. Commented Sep 22, 2011 at 13:58
  • Thanks, I added that but the error is still occuring. Edit: I have been reading the class documentation. This is how I got this far but as far as my untrained eye can tell there is not much wrong with my code sample. Commented Sep 22, 2011 at 13:59
  • The (object) bit just makes it a new-style class which makes it behave better in some situations (read about new-style classes for more info). It won't change your problem here, but it's a good habit to get into. Commented Sep 22, 2011 at 14:00

4 Answers 4

2

That is not problem with import. Since color_switcher is not static method, you must first create class instance, and only then call a member function:

if event.type == pygame.MOUSEBUTTONDOWN:
     menus.Menu().color_switcher(counter, screen)

Alternatively, you can declare your class as

class Menu:
    @staticmethod
    def color_switcher(counter, screen):

and then use it as menus.Menu.color_switcher(counter, screen)

Sign up to request clarification or add additional context in comments.

1 Comment

It's a method, not a function, and you can also call class methods on the class.
2

I then wanted to put the function in a class so I can get up and running with classes

It's not that simple.

You really, really, really need to do a complete Python tutorial that shows how to do object-oriented programming.

You rarely call a method of a class. Rarely.

You create an instance of a class -- an object -- and call methods of the object. Not the class. The object.

x = Menu()
x.color_switcher(counter, screen)

Comments

1

You're trying to call an instance method as a class method.

Two solutions:
1) change the client code: call the method on an instance of the class

menus.Menu().color_switcher(counter, screen) # note the parentheses after Menu

2) change the definition: change the instance method to a class method using the class method annotation

Comments

1

You need to create an instance of Menu before you can call the method. For example:

my_menu = Menu()
my_menu.color_switcher(counter, screen)

You are currently treating color_switcher as if it is a class method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.