Sign in to follow this  
VanillaSnake21

Python class question

Recommended Posts

I'm starting out python and I'm pretty surprised that it's harder then what I heard everyone else say it was. What is this "self" thing I have to include as a parameter of every function? The upsetting thing is that I cannot put the definition together no matter how many tutorials I read. Is it like "this" pointer in c++? Also how do I create data members in the class? From the examples I've seen they just say self.velocity = something, but don't specify velocity anywhere in the class. Is data automatically created once I reference it from self. Maybe I'm over thinking everything, the language probably looks very easy when you're not coming from c++ when you just accept everything as a given.

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
What is this "self" thing I have to include as a parameter of every function? ... Is it like "this" pointer in c++?
You've got it in one. Although you only have to include it for class methods, as opposed to free functions.

Quote:
Is data automatically created once I reference it from self.
Again, that's right [smile] The constructor is usually a good place to go looking for class members, it doesn't have to be the definitive source though; members can be bound to an instance object at any time by anybody and anything!

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Quote:
Is data automatically created once I reference it from self.
Again, that's right [smile] The constructor is usually a good place to go looking for class members, it doesn't have to be the definitive source though; members can be bound to an instance object at any time by anybody and anything!


Interesting, pretty wild stuff, lol. I would have actually assumed that that was the case if I didn't get an error a few hours ago when I referenced self.vx. "AttributeError: Box instance has no attribute 'vx'" It went away after I "declared" the variables as vx, vy = 0,0 Why would that happen?

Share this post


Link to post
Share on other sites
The comprehensive answer.


The short answer:
Quote:
Original post by VanillaSnake21
What is this "self" thing I have to include as a parameter of every function? The upsetting thing is that I cannot put the definition together no matter how many tutorials I read. Is it like "this" pointer in c++?

Pretty much. In C++ a member function is given the default calling convention __thiscall, meaning that it takes a "hidden" first parameter that is the pointer to the instance. Similarly, in Python, member functions take a first parameter that is the instance, except unlike in C++ the first parameter is not hidden - it is explicit. Consider:
class X:
def __init__(self, initializer):
self.value = initializer

def f(self):
print self.value

x = X(45)
x.f() # prints 45
X.f(x) # also prints 45


Quote:
Original post by VanillaSnake21
Maybe I'm over thinking everything, the language probably looks Also how do I create data members in the class? From the examples I've seen they just say self.velocity = something, but don't specify velocity anywhere in the class. Is data automatically created once I reference it from self.

Not even just from self. A new data member is created once you reference it at any point in the instance's lifetime:
class Y:
def f(self):
print self.value

y = Y()
y.f() # raises AttributeError: Y instance has no attribute 'value'
y.value = 42
y.f() # prints 42

It gets freakier:
class Z:
def __init__(self, initializer):
self.value = initializer

def f(self):
print self.value

z = Z(5)
del z.value
z.f() # raises AttributeError: Z instance has no attribute 'value'

Python objects are extremely flexible. You can add or remove a method to/from a class or change the definition of a method in a class at runtime (though you can't do this with the built-in types because they're implemented in C and are immutable). The Python notion of a class is quite more amorphous than the C++ concept.

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
Interesting, pretty wild stuff, lol. I would have actually assumed that that was the case if I didn't get an error a few hours ago when I referenced self.vx. "AttributeError: Box instance has no attribute 'vx'" It went away after I "declared" the variables as vx, vy = 0,0 Why would that happen?

You made them class variables.
class X:
shared = 12

def __init__(self, i):
self.value = i

x = X(9)
y = X(5)
x.shared # prints 12
x.value # prints 9
y.shared # prints 12
y.value # prints 5

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi

Not even just from self. A new data member is created once you reference it at any point in the instance's lifetime:
class Y:
def f(self):
print self.value

y = Y()
y.f() # raises AttributeError: Y instance has no attribute 'value'
y.value = 42
y.f() # prints 42


But why wasn't it created during init, when you said self.value? Maybe a more refined definition would be that data is appended to the class only when it is being written to (in the __init__ it's beign read)

Quote:

It gets freakier:
[code]class Z:
def __init__(self, initializer):
self.value = initializer

def f(self):
print self.value

z = Z(5)
del z.value


I can see why the error is thrown here, you're removing value from the the class, rendering it undefined for the f().

Thanks, that was a pretty good explanation, I figured it was something like an explicit this pointer. I'm sure this luxuriously liberal language won't take much time getting used to [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
But why wasn't it created during init, when you said self.value? Maybe a more refined definition would be that data is appended to the class only when it is being written to (in the __init__ it's beign read)

You're not paying enough attention. Y has no __init__() function. That was done deliberately to emphasize that you don't need __init__() if your class does no setup, and you can inject new members (including functions, via alias or via lambda expressions) at any time.

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
I'm starting out python and I'm pretty surprised that it's harder then what I heard everyone else say it was. ...


Of course you have to learn the language properly first. No programming language is gonna be that easy, that you don't even have to learn it first. But once you have, it is easy and convenient compared to other languages. Just give it a chance. Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this