<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.6049" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>
<DIV>Well - this is all timely email. I just spent the day configuring my
HP mini netbook running Windows 7 with all the right software based on
recomendations from folks on this list, from the Python Tutor list and an email
group of former colleagues where I spelled out exactly all the programming
languages and stuff I ideally wanted. It has been very enlightening and
actually not so difficult! Asking pays off! And I made a few
realizations - I had some misperceptions about some software packages and -- I
had been thinking about this 'self' business since yesterday. And you and
Ethan and Ben Finney from yesterday (the last email msg I read last night, might
know) are right -- I was being stubborn and wanting to do things *my* way and I
was telling myself that when it comes time to do this for a future employer,
well I will just change my code in the appropriate places to 'self' and will
start using 'self' when I start working for them. And a little
thought in the back of my head ... I wonder if the employer would be a
little annoyed by that :) </DIV>
<DIV> </DIV>
<DIV>OK - I need my hand slapped sometimes and I see that you all are trying to
prevent me from developing a bad habit. A few comments below:</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>"Steven D'Aprano" <<A
href="mailto:steve+comp.lang.python@pearwood.info">steve+comp.lang.python@pearwood.info</A>>
wrote in message <A
href="news:4d453127$0$29965$c3e8da3$5496439d@news.astraweb.com">news:4d453127$0$29965$c3e8da3$5496439d@news.astraweb.com</A>...</DIV>
<DIV>> On Sat, 29 Jan 2011 10:39:49 -0800, patty wrote:<BR>> <BR>>>
I am glad you said this. I have been avoiding understanding
this<BR>>> 'self', just accepting it :} For the time being, since my
programs I am<BR>>> creating are for my own use, I think I will make my
own names up, that<BR>>> are descriptive to me as the programmer, it's all
going to be<BR>>> interpreted anyway. And the other email equating
to C's argv, etc. -<BR>>> now I get it.<BR>> <BR>> <BR>> That's a
shame, because `self` is actually very simple once you <BR>> understand the
basic principles of object-oriented programming.<BR>> <BR>> What names
would you choose? Unless you're writing descriptors, or using <BR>> class
methods, both of which should be considered advanced usage (highly <BR>>
advanced for descriptors, moderately so for class methods), it's not like
<BR>> every method needs a different descriptive first argument. In English,
<BR>> "self", "this", "me" or "instance" would be good names. What else would
<BR>> you use?</DIV>
<DIV> </DIV>
<DIV>That is the thing, I can get quite creative. I envisioned myself
giving it a name something like 'instancecalledfrommain' probably
scrunched up to 'instfrmmain'</DIV>
<DIV>Weird descriptive names like that. I keep thinking of it as a
variable, so I keep thinking of what it is used for underlying its name.
Back to the I-can-do-anything-I-want</DIV>
<DIV>mindset. </DIV>
<DIV> </DIV>
<DIV>I have saved the messages in this thread and other ones from the last month
or so about 'self' so I think I will gather them together and read them more
carefully including your very good explanations below.</DIV>
<DIV> </DIV>
<DIV><BR>> <BR>> <BR>> The idea of method syntax is that you start with
an instance of some <BR>> class:<BR>> <BR>> mystring = "hello
world" # an instance of the str class<BR>> <BR>> In procedural
languages like C or Pascal, you would call a function and <BR>> give the
string as an argument. Python supports this programming model, <BR>> and uses
it for built-ins like len:<BR>> <BR>> len(mystring)<BR>> => returns
11<BR>> <BR>> </DIV>
<DIV> </DIV>
<DIV>And I am more comfortable in the C and Pascal worlds as a base so I see why
I go this direction. </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><BR>> Object oriented programming uses a different syntax. Instead of
<BR>> <BR>> function(instance)<BR>> <BR>> as above, we take the
instance argument outside the brackets. For example:<BR>> <BR>>
mystring.upper() # instead of upper(mystring)<BR>> => returns "HELLO
WORLD"<BR>> <BR>> If there are any extra arguments needed, they go inside
the brackets as <BR>> normal.<BR>> <BR>> So far, this is just a change
of syntax. It's like saying "The cat of my <BR>> brother's" vs. "my brother's
cat" -- the meaning is the same, but the <BR>> syntax differs. </DIV>
<DIV> </DIV>
<DIV>And I am a linguist so now you are Really talking ;)</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>The real advantages of object oriented programming and <BR>> methods
come elsewhere (e.g. encapsulation and inheritance).<BR>>
<BR>> [Aside: when I was learning this, the hardest part I
found was<BR>> remembering which things were functions, and
which were methods. <BR>> I kept writing (wrongly!) things
like:<BR>> <BR>> "hello
world".len()<BR>> upper("hello world")<BR>>
<BR>> Unfortunately there is no easy way to recognise what
will be a<BR>> function like len, and which are methods
like upper. That will <BR>> come with experience.</DIV>
<DIV> </DIV>
<DIV>Oh! I do this! I seem to use this
syntax interchangably. I really need to memorize this
carefully.</DIV>
<DIV> </DIV>
<DIV><BR>> <BR>> Back to function/procedural syntax versus object oriented
syntax... <BR>> <BR>> One difference, though, is when you write a method
definition. Because <BR>> the method you write starts off life as an ordinary
function, you need to <BR>> write it *as if* it were a function you call like
len() above. Here's how <BR>> you might write a method in a class:<BR>>
<BR>> class MyClass:<BR>> def method(self,
extra):<BR>> pass<BR>> <BR>>
When you then call the method:<BR>> <BR>> instance = MyClass()<BR>>
instance.method("something extra")<BR>> <BR>> Python automatically changes
the syntax for you, and passes two arguments <BR>> to the function as if you
did this:<BR>> <BR>> # pseudo-code<BR>> set self = instance<BR>> set
extra = "something extra<BR>> extract "method" from MyClass<BR>> call
method(self, extra)<BR>> <BR>> We call the first argument something like
"self" because it will ALWAYS <BR>> be the instance itself. Unlike a regular
function, which can have <BR>> anything passed as the first argument, and
therefore you should give it a <BR>> descriptive name, the method's first
argument is provided automatically <BR>> for you and will always be the
instance you started with.<BR>> <BR>> <BR>> <BR>> I hope this
helps.</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>Immensely<BR>> <BR>> <BR>> <BR>> <BR>> -- <BR>>
Steven<BR>> -- <BR>> <A
href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</A><BR>>
<BR>></DIV>
<DIV> </DIV>
<DIV>Wow! Thanks so much! </DIV>
<DIV> </DIV>
<DIV>Patty</DIV></FONT></DIV></BODY></HTML>