RE: when and how do you use Self?

2005-11-07 Thread Tieche Bruce A MSgt USMTM/AFD
Well, thanx for all the ... useful information.

I thought that I would try, but this has turned out to be a waist of my time.

Have fun playing with your egos 

-Original Message-
From: Bruno Desthuilliers [mailto:[EMAIL PROTECTED] 
Sent: Thursday, November 03, 2005 9:29 PM
To: python-list@python.org
Subject: Re: when and how do you use Self?

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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-07 Thread Fredrik Lundh
Tieche Bruce A MSgt USMTM/AFD [EMAIL PROTECTED] wrote:

 Well, thanx for all the ... useful information.

 I thought that I would try, but this has turned out to be a waist of my time.

did you perhaps miss that at least three people wrote proper replies to your 
post?

http://article.gmane.org/gmane.comp.python.general/429472
http://article.gmane.org/gmane.comp.python.general/429703
http://article.gmane.org/gmane.comp.python.general/429858

(if you read the newsgroup in a threaded reader, spotting them should be
relatively easy).

/F



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-07 Thread Magnus Lycka
Tieche Bruce A MSgt USMTM/AFD wrote:
 Well, thanx for all the ... useful information.
 
 I thought that I would try, but this has turned out to be a waist of my time.

As with all studying, it might be somewhat time consuming to
filter out the useful stuff in a sea of information. You did
get correct and useful advice from two Python luminaries. If
you can't see that in all the noise, it's all your loss.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-07 Thread Bruno Desthuilliers
Tieche Bruce A MSgt USMTM/AFD a écrit :
 Well, thanx for all the ... useful information.
 
 I thought that I would try, but this has turned out to be a waist of my time.
 
 Have fun playing with your egos 

s/your egos/words/

If you can't stand a joke (possibly very bad, but that's another point), 
I'd say the ego problem is on your side. In case you didn't notice, this 
joke was not directed to you personally.

BTW, this is (usually) a very newbie-friendly newsgroup (at least when I 
resist the temptation to reply with stupid puns). Instead of RTFMs, 
you've got helpful answers by peoples like Fredrik Lundh and Alex Martelli.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-07 Thread Ron Adam

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 ([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











-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-04 Thread Tony Nelson
In article [EMAIL PROTECTED],
 Tieche Bruce A MSgt USMTM/AFD [EMAIL PROTECTED] 
 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.:'[EMAIL PROTECTED]
  '  http://www.georgeanelson.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


when and how do you use Self?

2005-11-03 Thread 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])

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Fredrik Lundh
Tieche Bruce A MSgt USMTM/AFD [EMAIL PROTECTED] wrote:

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

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

consider a class C:

 class C:
... def method(self):
... print self
...
 C
class __main__.C at 0x0091D7E0

you can create unique instances of this class by calling the class itself:

 a = C()
 a
__main__.C instance at 0x00925FD0

 b = C()
 b
__main__.C instance at 0x00927030

here, a and b are two separate objects, that both refer to the same
class object, but are otherwise distinct (the cryptic codes are object
identities).

now, if you call a method on one of those objects, Python will use the
method code from the class, but the method will get a reference to the
instance as its first argument (self).

when you call the method via the a object, the method gets a reference
to the a object as the first argument:

 a.method()
__main__.C instance at 0x00925FD0

when you call the method via the b object, the method gets a reference
to the b object as the first argument:

 b.method()
__main__.C instance at 0x00927030

the instance object is usually used to store instance-specific variables
(usually known as attributes or members).  an example:

 class Counter:
... def __init__(self):
... self.value = 0
... def increment(self):
... self.value = self.value + 1
... return self.value
...
 a = Counter()
 b = Counter()
 a.increment()
1
 a.increment()
2
 a.increment()
3
 b.increment()
1

(the __init__ method is automatically called for each new instance)

you can also access the instance attributes from the outside:

 print a.value
3
 print b.value
1

for more on this, see:

http://docs.python.org/tut/node11.html#SECTION001130

/F



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread bruno at modulix
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 '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread 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.

On 03/11/05, bruno at modulix [EMAIL PROTECTED] 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 '[EMAIL PROTECTED]'.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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread bruno at modulix
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 [EMAIL PROTECTED] 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 '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Dan Sommers
On Thu, 3 Nov 2005 10:48:28 -0500,
Chris Cioffi [EMAIL PROTECTED] 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/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Steven D'Aprano
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread bruno at modulix
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 '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Jorge Godoy
bruno at modulix [EMAIL PROTECTED] 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  [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Jeffrey Schwab
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Steven D'Aprano
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Bruno Desthuilliers
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...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Bruno Desthuilliers
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).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when and how do you use Self?

2005-11-03 Thread Alex Martelli
Tieche Bruce A MSgt USMTM/AFD [EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list