473,230 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

when and how do you use Self?

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
14 3151
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
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
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
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
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
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
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
33
by: Xah Lee | last post by:
i've started to read python tutorial recently. http://python.org/doc/2.3.4/tut/tut.html Here are some quick critique: quick example: If the input string is too long, they don't truncate it,...
6
by: Coral Snake | last post by:
I am having problems with programming even simple "Hello World" programs from books and tutorials that use Python GUI libraries. Such Programs cause python to throw "Attribute Errors" even when the...
0
by: sector119 | last post by:
I use python 2.4.1 and PIL 1.1.5 and when I execute my program I got error: ../code.py Traceback (most recent call last): File "./code.py", line 7, in ? class DrawPlus(ImageDraw.Draw):...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
1
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?!...
2
by: cfriedalek | last post by:
Sorry for the vague subject. Not sure what the right terminology is. How can I use an instance's data by reference to the instance name, not the instance attribute? OK the question is probably...
3
by: dunleav1 | last post by:
In 9.1 and 9.5 (Linux 64 bit) when a buffer pool is set to self- tuning, how are blocks configured in respect to blocked vs non-blocked when self-tuning is set to on? (ie) I have one bufferpool...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.