By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

when and how do you use Self?

P: n/a
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 (br**********@usmtm.sppn.af.mil)

Nov 3 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

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

Don't use self. Use other.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 3 '05 #2

P: n/a
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.

On 03/11/05, bruno at modulix <on***@xiludom.gro> wrote:
Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

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

Don't use self. Use other.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on***@xiludom.gro'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list

--
"A little government and a little luck are necessary in life, but only
a fool trusts either of them." -- P. J. O'Rourke
Nov 3 '05 #3

P: n/a
On Thu, 3 Nov 2005 10:48:28 -0500,
Chris Cioffi <ev********@gmail.com> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 3 '05 #4

P: n/a
Chris Cioffi wrote:
<ot>
as a point of style, top-posting is a Bad Thing(tm)
(fixed)
</ot>

On 03/11/05, bruno at modulix <on***@xiludom.gro> wrote:
Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

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

Don't use self. Use other.

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
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on***@xiludom.gro'.split('@')])"
Nov 3 '05 #5

P: n/a
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:
Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

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

Don't use self. Use other.


Are you serious? You don't recommend doing this?

def MyClass:
def __init__(self, x):
self.x = x

???
--
Steven.

Nov 3 '05 #6

P: n/a
Steven D'Aprano wrote:
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:

Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

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


Don't use self. Use other.

Are you serious?


Are you seriously wondering if I am serious ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 3 '05 #7

P: n/a
bruno at modulix <on***@xiludom.gro> writes:
Don't use self. Use other.

Are you serious?


Are you seriously wondering if I am serious ?


Hmmm... I hope there's no deadlock in this loop...

--
Jorge Godoy <go***@ieee.org>
Nov 3 '05 #8

P: n/a
bruno at modulix wrote:
Steven D'Aprano wrote:
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:
Tieche Bruce A MSgt USMTM/AFD wrote:
I am new to python,

Could someone explain (in English) how and when to use self?
Don't use self. Use other.

Are you serious?

Are you seriously wondering if I am serious ?


I was also wondering. What's the problem you see with the identifier
"self?"
Nov 3 '05 #9

P: n/a
On Thu, 03 Nov 2005 20:19:03 +0100, bruno at modulix wrote:
Steven D'Aprano wrote:
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:

Tieche Bruce A MSgt USMTM/AFD wrote:

I am new to python,

Could someone explain (in English) how and when to use self?
Don't use self. Use other.

Are you serious?


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.
--
Steven.

Nov 3 '05 #10

P: n/a
Jeffrey Schwab a écrit :
bruno at modulix wrote:
Steven D'Aprano wrote:
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:

Tieche Bruce A MSgt USMTM/AFD wrote:

> I am new to python,
>
> Could someone explain (in English) how and when to use self?

Don't use self. Use other.

Are you serious?
Are you seriously wondering if I am serious ?


I was also wondering.


You shouldn't.
What's the problem you see with the identifier
"self?"


It's just to sale fish...
Nov 3 '05 #11

P: n/a
Steven D'Aprano a écrit :
On Thu, 03 Nov 2005 20:19:03 +0100, bruno at modulix wrote:

Steven D'Aprano wrote:
On Thu, 03 Nov 2005 10:14:23 +0100, bruno at modulix wrote:

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

Don't use self. Use other.
Are you serious?


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.


Guilty. I thought the pun would be obvious (even if very bad).
Nov 4 '05 #12

P: n/a
Tieche Bruce A MSgt USMTM/AFD <br**********@usmtm.sppn.af.mil> wrote:
...
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
Nov 4 '05 #13

P: n/a
In article <ma*************************************@python.or g>,
"Tieche Bruce A MSgt USMTM/AFD" <br**********@usmtm.sppn.af.mil>
wrote:
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


<http://docs.python.org/tut> is a good explanation of just about all of
Python. You should read it. It explains when to use "self".
__________________________________________________ ______________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
Nov 4 '05 #14

P: n/a

Tieche Bruce A MSgt USMTM/AFD wrote:
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 (br**********@usmtm.sppn.af.mil)

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



Nov 7 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.