1

I downloaded a program to test on the laptop that only has python 2.4.4 on it and it keeps telling me syntax error on the parentheses of class main(): I have no experience with classes, so I am looking for a quick fix for this problem. How are classes different in python 2?

class main():
    def __init__(self):
        response=self.valid_input("New game or Load game?",["load","new"])
        if response == "load":

the syntax is always on the ( part.

2 Answers2

2

I don't have a python2.4 interpreter to test this, but it seems that python2.4 you either don't use parenthesis class main: or you must specify at least one class to inherit from class main(object):

https://docs.python.org/release/2.4.4/ref/class.html

Augusto Hack
  • 2,032
  • 18
  • 35
  • What are the () used for in later versions if they wouldn't be needed in 2.4.4? –  Oct 09 '15 at 20:43
  • Careful with this, the two styles of classes do not share the same object model. – saarrrr Oct 09 '15 at 20:52
  • @DeliriousMistakes just a side note, there are indeed two models, the new-style classes were introduced on [version 2.2](https://www.python.org/doc/newstyle/), that is not the problem that you are experiencing! You have a syntax error, python2.7 allows the syntax `class A():` [this is the docs](https://docs.python.org/2/reference/compound_stmts.html#class-definitions), note the use of brackets around the expression list. – Augusto Hack Oct 09 '15 at 21:23
2

In python 2, There are two styles of classes, old and new, and they are different and not totally compatible with each other. In order to get new style classes (think classic OO class), they must explicitly inherit from object. Omitting the object inheritance is valid syntax but the class concept is not the same. So use:

class main(object): and know that it is not the same as class main:

In python 3, the object inheritance is implicit, so:

class main: is the same as class main(object): and is a new style class.

You should code with new style classes, as that is the future of Python and the only class style available in 3. See here for more detailed information. Python class inherits object

Community
  • 1
  • 1
saarrrr
  • 2,754
  • 1
  • 16
  • 26