Quantcast

Jump to content


Photo

Python: Can someone please explain Self to me?

python newb 2.7

  • Please log in to reply
7 replies to this topic

#1 DregsandDregs

DregsandDregs
  • 127 posts

Posted 09 February 2015 - 07:07 PM

First off, I have a bad habit of taking apart the code examples in the book I'm learning from and putting it together and taking it apart so I can understand it.  If I don't understand it I'm really shitty about using it correctly--this is probably why I keep on dropping my stupid colons.  This normally good habit is a bad thing because I'm not advanced enough to know what I'm pulling apart and it made me ragequit for a while.

 

Right now I'm just taking it as structural, white spaces define blocks (and despite that you still need colons), and when defining things in classes you need to have have (self) up there along with your other variables.

 

I understand there's actually a technical reason for it, Guido van Rossum actually blogged about why, and my friend says it makes sense even if he doesn't like it.  However, I can't quite grok it right. 

 

What I get from it that (self) is for when you actually get to put some data into your method, because some stuff is static and some is dynamic. 

 

But when asking for other variables, (self, x, y), that's only asking for 2 data entries, so it's not asking for self?  And I know you can't just put self in, and no x, if you want to manipulate some forms of data, like a basic print string.

 

Lost newb is lost.



#2 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 09 February 2015 - 07:12 PM

When you create a new class, self is that object that you are trying to access... examples....

class Foo(object):
    def __init__(self, var):
        self.var = var

x = Foo("a")
x.var
>> "a"

y = Foo("b")
y.var
>> "b"
Something like that....

#3 Josh

Josh
  • 318 posts

Posted 09 February 2015 - 08:06 PM

When Python calls a method in your class it always passes a pointer to the current instance of the class. You can test this theory by excluding all parameters in a method (that's not explicitly defined as static):

>>> class A:
...   def test():
...     pass
... 
>>> a = A()
>>> a.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test() takes 0 positional arguments but 1 was given

Also, 'self' is just a standard. It's not actually required to be called that:

>>> class B:
...   def test(lol):
...     lol.cool = 'awesome'
... 
>>> b = B()
>>> b.test()
>>> b.cool
'awesome'

It's best to think of it as a pointer being passed rather than a keyword with special properties. 

 

If you want some examples of OOP fundamentals as it pertains to Neopets, feel free to browse the library linked in my signature. Also, if you'll be doing any Neopets stuff in Python, I would start there rather than reinventing the wheel :)


Edited by Josh, 09 February 2015 - 08:10 PM.


#4 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 09 February 2015 - 08:15 PM

Same shit :p it's kinda second nature to me :p

#5 Josh

Josh
  • 318 posts

Posted 09 February 2015 - 09:09 PM

Same shit :p it's kinda second nature to me :p

 

Yeah, there was nothing wrong with your example :p. I just happened to recently teach a small group of individuals on some Python basics and a few of them were really getting stuck on 'self' so I had to come up with an intuitive way to help show how the internals worked (a lot of them came from a C background so pointers made sense). 


Edited by Josh, 09 February 2015 - 09:10 PM.


#6 DregsandDregs

DregsandDregs
  • 127 posts

Posted 10 February 2015 - 01:05 PM

When you create a new class, self is that object that you are trying to access... examples....
 

class Foo(object):
    def __init__(self, var):
        self.var = var

x = Foo("a")
x.var
>> "a"

y = Foo("b")
y.var
>> "b"
Something like that....

 

 

I see the example, and I don't understand it.

 

When Python calls a method in your class it always passes a pointer to the current instance of the class. You can test this theory by excluding all parameters in a method (that's not explicitly defined as static):

>>> class A:
...   def test():
...     pass
... 
>>> a = A()
>>> a.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test() takes 0 positional arguments but 1 was given

Also, 'self' is just a standard. It's not actually required to be called that:

>>> class B:
...   def test(lol):
...     lol.cool = 'awesome'
... 
>>> b = B()
>>> b.test()
>>> b.cool
'awesome'

It's best to think of it as a pointer being passed rather than a keyword with special properties. 

 

If you want some examples of OOP fundamentals as it pertains to Neopets, feel free to browse the library linked in my signature. Also, if you'll be doing any Neopets stuff in Python, I would start there rather than reinventing the wheel :)

 

Okay this makes some more sense, basically self is, as you said, a holder for a pointer.  Sort of, "Yes this can be accessed," sort of thing?

 

As to Neopets...

 

Oh boy.  I've not been on Neopets in...Okay, I got annoyed when I wasn't collecting intrest automatically anymore and had to go and click the button daily.  But then they turned my Tatsu into a bloody gryffin and I completely lost it.  Yes it was a cute and fluffy green griffin, and I liked griffins, but I Picked A TATSU.  But there were no more Tatsus.  So I was sad, and left.

 

If I do program, it will probably end up being for FR.  Though who knows, maybe I'll give a swing into Neopets once more without the childish rage.  Who knows, I might even find my login information from so long ago.



#7 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 10 February 2015 - 01:47 PM

Hop on irc and i can explain it easier if you still want help :p

http://www.neocodex.us/forum/ircChat/



#8 Josh

Josh
  • 318 posts

Posted 10 February 2015 - 04:00 PM

I see the example, and I don't understand it.

 

 

Okay this makes some more sense, basically self is, as you said, a holder for a pointer.  Sort of, "Yes this can be accessed," sort of thing?

 

As to Neopets...

 

Oh boy.  I've not been on Neopets in...Okay, I got annoyed when I wasn't collecting intrest automatically anymore and had to go and click the button daily.  But then they turned my Tatsu into a bloody gryffin and I completely lost it.  Yes it was a cute and fluffy green griffin, and I liked griffins, but I Picked A TATSU.  But there were no more Tatsus.  So I was sad, and left.

 

If I do program, it will probably end up being for FR.  Though who knows, maybe I'll give a swing into Neopets once more without the childish rage.  Who knows, I might even find my login information from so long ago.

 

Here's another example to help solidify it:

>>> class A:
...   someVar = 'some value'
...   
...   def test(self):
...     self.someVar = 'some new value'
... 
>>> class B:
...   someVar = 'some value'
>>> def change_value(obj):
...   obj.someVar = 'some new value'
>>> a = A()
>>> a.someVar
'some value'
>>> a.test()
>>> a.someVar
'some new value'
>>> b = B()
>>> b.someVar
'some value'
>>> change_value(b)
>>> b.someVar
'some new value'

The test() method and change_value function above essentially do the same thing. The test() method uses the pointer to the object instance passed by Python to change a property in the A class. The change_value() function uses a pointer to the object instance that was passed by you to change a property in the B class. 

In order to support the OOP model for programming a class method must be able to change state and this requires having a pointer to the class instance. If this completely confuses you, I would spend a little time looking into the basics of Object Oriented Programming (OOP).


Edited by Josh, 10 February 2015 - 04:01 PM.




Also tagged with one or more of these keywords: python, newb, 2.7

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users