when and how do you use Self?

  • Thread starter Tieche Bruce A MSgt USMTM/AFD
  • Start date
T

Tieche Bruce A MSgt USMTM/AFD

I am new to python,



Could someone explain (in English) how and when to use self?



I have been reading, and haven't found a good example/explanation





Bruce Tieche ([email protected])
 
C

Chris Cioffi

As a point of style: the 'other' identifier should only be used in
Zen Metaclass programming as an implicit reference to the calling
object or as a list of references to all other instances of the class.
Context will make it both clear and obvious which use case is
desired.
 
D

Dan Sommers

As a point of style: the 'other' identifier should only be used in
Zen Metaclass programming as an implicit reference to the calling
object or as a list of references to all other instances of the class.
Context will make it both clear and obvious which use case is desired.

Can I use the 'other' identifier in, e.g., an __add__ method?

Please? ;-)

Regards,
Dan
 
B

bruno at modulix

Chris Cioffi wrote:
<ot>
as a point of style, top-posting is a Bad Thing(tm)
(fixed)
As a point of style: the 'other' identifier should only be used in
Zen Metaclass programming as an implicit reference to the calling
object or as a list of references to all other instances of the class.

As a point of style, if it refers to a list, it should be 'others' and
not 'other'.

Also, this was supposed to be a joke. I can well understand that my sens
of humour is somewhat disastrous and that this was not a _good_ joke,
but "context should have made both clear and obvious" that it was one.
Context will make it both clear and obvious which use case is
desired.

import this
 
S

Steven D'Aprano

Are you seriously wondering if I am serious ?

Well, you are either serious, or you're guilty of giving extremely bad
advice to a newbie who will probably have even less ability to recognise
an in-joke than I do.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :
Well, you are either serious, or you're guilty of giving extremely bad
advice to a newbie who will probably have even less ability to recognise
an in-joke than I do.

Guilty. I thought the pun would be obvious (even if very bad).
 
A

Alex Martelli

Tieche Bruce A MSgt USMTM/AFD said:
I am new to python,
Could someone explain (in English) how and when to use self?

A class's methods use 'self' to refer to the object (instance of the
class) they're being called on; mostly, they access (get or set)
attributes on self, and/or call other methods on self.

I hope that's English enough for you. Here's a simple example:

class Struggle(object):
def __init__(self, value): self.value = value
def __str__(self): return 'Struggle(%r)' % self.value

Class Struggle has two (special) methods, an initializer and a
transformer to string. Each uses 'self' to refer to the instance on
which it's being called -- specifically, to set or get the 'value'
attribute. So, when I code:

x = Struggle(23)
print x

I obtain the output:

Struggle(23)

In this case, the 'self' inside each method refers to the same object to
which the name 'x' refers ``on the outside''.


Alex
 
T

Tony Nelson

R

Ron Adam

Tieche said:
I am new to python,

Could someone explain (in English) how and when to use self?

I have been reading, and haven't found a good example/explanation


Bruce Tieche ([email protected])


Hi, Sometimes it's hard to get a simple answer to programming questions
as everyone sees different parts of the elephant. ;-)


The use of self is needed because methods in class's are shared between
all the instances (objects created from class's). Because of this
sharing, each method in a class needs a name to receive the specific
instance reference which called it.

If every instance had it's own copy of all the methods in a class, we
might not need 'self', but our programs would become extreme memory
hogs. So sharing code is the great benefit of class's.

For example...

class myclass(object):
def foo(self, a, b):
self.c = a + b

The method foo is defined but not executed until it is called later from
an instance. It's located in the class, but may be called from a lot,
(thousands or more), different instances made from this class.

bar = myclass() # create a single instance (object)
# and bind it to the name bar.


Then when you do...

bar.foo(1,2) # converted to -> myclass(bar, 1, 2)

It calls the 'foo' method located in the parent class and pass's a
reference to 'bar' as the first argument. 'self' becomes the new name
for bar within foo.

self.c = a + b # same as -> bar.c = a + b


This should be enough to visualize the basic relationship. Hope it helped.

Cheers,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top