Forum Archive

class instances

snarl_barx231

I'm just learning python and oop in particular so I've got some basic questions.

Why doesn't this work:

class Foo:
def init(self, x, y):
self.x = x
self.y = y

bar = Foo(42, 7) <------ here i get the error "this constructor takes no arguments"

print bar.x

hmmmmmm....for some reason this post was stripped of indentation

Thanks!

Gcarver166

I think you need def init(self, x, y):. Looks like you only have 1 underscore on each side of init.

pudquick51

@Gcarver is correct. The init method for a class has two underscores on each side.

@snarl_barx - If you want to preserve your indentation, next time start a block of code with <PRE> and end it with </PRE> and it will end up looking like this:

class Foo:
    def __init__(self, x, y):
        self.x = x
        self.y = y