0

I'm using python to define a class, then append an instance of it to a list.

class town:
    def __init__(name_, x_, y_, mayor_):
        name = name_
        main_x = x_
        main_y = y_
        mayor = mayor_
        desc = desc_

def add_town(name_, x_, y_, mayor_):
    towns.append(town(name_, x_, y_, mayor_))
    town_number += 1

def onCommand():
    add_town(args[1], loc_x, loc_y, sender.getName())

onCommand()

Unfortunately, I get this error when add_town is executed:

Caused by: Traceback (most recent call last): File "", line 95, in onCommandTown File "", line 74, in add_town TypeError: init() takes exactly 4 arguments (5 given)

Note: this is a shorted version of the code I'm using to keep things simple. Rest assured that all variables are defined correctly.

EDIT: Also, towns is a list.

Does anyone know why this error is here? I've been puzzling over it for half an hour and nothings happening...

2

1 Answer 1

3

change your __init__ from

def __init__(name_, x_, y_, mayor_):

to

def __init__(self, name_, x_, y_, mayor_):

Also, you might have to make the initialized parameters class variables.

def __init__(self, name_, x_, y_, mayor_):

    self.name = name_
    self.main_x = x_
    self.main_y = y_
    self.mayor = mayor_
    self.desc = desc_

and access it as self.name, etc within the class methods.

Read more on this here

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

1 Comment

Thanks very much all, that worked perfectly. Is there some way of defining a "self" namespace so that I don't have to constantly type self.x ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.