[Tutor] Fwd: Self, Scopes and my unbelievable muddleheadedness.

doug shawhan doug.shawhan at gmail.com
Thu Oct 26 21:19:32 CEST 2006


---------- Forwarded message ----------
From: doug shawhan <doug.shawhan at gmail.com>
Date: Oct 26, 2006 1:45 PM
Subject: Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.
To: Alan Gauld <alan.gauld at btinternet.com>



On 10/26/06, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "doug shawhan" <doug.shawhan at gmail.com> wrote
> > I'm having a rather difficult time understanding the proper use of
> > "self".
>
> Doug, I think you may be having a more fundamental problem.
> Looking at your code at the most superficial level it seems
> you may not understand objects.


I've always been confused by much of the OOP terminology. I'm about to go
off on a tangent below regarding your explaination. I think it is helping
me, but I want to be sure. Bear with me. :-)

My original question should have been:

"Why is this dictionary coming back weird?" The dictionary was just a
placeholder for a future .ini file to be read with ConfigParser. In playing
around with different ideas. I thought "I'd like to put each particular set
of activities into modules anyway, so let's just figure out how that might
work".

 I ran onto the problem of the dictionary being changed globally, when I
thought that use of "self" meant that any changes to that data were limited
to the scope of the method in that particular class (and it's resultant
object), thereby insulating it from other methods in the same class and
other objects which inherit from the same class"  (Don't reply to this yet!
:-)

I'm sure that I have simply thought that my global declaration was not going
to be changed in place by misunderstanding what "self" means. My confusion
stemmed from just how weird the change appeard to be. I.e. "But I just
looked at the dang thing and it was what I expected! Where dem extra
parentheis come from? Huh?"

I apologize for my incoherence in asking the question. I was in the middle
of trying out different ideas and ran onto this weirdness and could not let
it go. I should taken the time to write a generic example, but frankly, I
was frustrated and was not thinking about how best to frame the question. It
was impolite of me to subject the list to my half-baked meanderings. Again,
sorry. You guys have helped so much and you deserve a little more effort on
my part.

So, fortified with 8 hours of sleep and 15mg of Adderall, here we go!


Classes are used to generate objects. Objects represent things.
> By logical deduction objects correspond to nouns in language.
> Objects do things when they receive messages (Possibly
> from other objects). Messages cause methods to be invoked,
> methods therefore correspond to verbs in language.
>

I felt like I was being told an integer is a "counting number" here. ;-)


> Looking at your code we find:
>
> class Create:   # a verb
>     def freshDB(self, DBPATH, Fields):   # a noun
>     def comparisonTable(self, DBPATH, Fields, columns, mode): #a noun
>
> In other words you have the concept inside out.
> you are trying to create Create objects and call Table and Database
> methods. It doesn't make sense.
>
> Normally we would expect to see DB and Table objects to which
> you send create messages.


I was even more confused. Were you objecting to my nomenclature?

So I thought, "If I named the class "Steve" and called the methods
createFreshDB() and createComparisonTable() so it would read:

"Steve, create a fresh database which includes tables named for the keys and
columns named for the values in this dictionary I'm handing you."

"Steve,  create a comparison table just like above, but with a different
key."

which would be followed (eventually, if I hadn't gotten bogged down) by

"Steve, tell gadfly to collate those two tables I just had you make and put
them in another table so I can generate a report"

Would that make any difference?

No especially in how the code was written (it was still ugly and half-baked,
and showing tons of bad habits), but I did suddenly realize what you meant
by "noun".

I have been treating objects like a corporate sales droid treats nouns.

I've been "Gifting" and "Tableing" the objects "TheGift" and "TheTable" in
my mind. (I fear I've also been "Impacting"  the  method  "impact()" without
asking  Steve.Please() first, (I.E. not quite understanding why my classes
within the modules were not showing up like I expected, but that's another
story.)

I've not been thinking of an object as a verb, rather more like a gerund.
Still wrong, but a different kind of wrong.

Now if you think of the objects as verbs then self is a confusing
> concept.
> But if you think of the objects as nouns then self is simply a
> reference
> to the object instance in question, a particular database or table.


Which leads me back to the "why is it there?" question I saw on the list
recently.

If "self" is simply a reference to the object instance in question, then why
does one have to basically mirror the variable at the top of the class?

The example I was working from is at:
http://www.penzilla.net/tutorials/python/modules/

# Intro To Python:  Modules
# book.py

"""
Class:  Book( title, author, keywords )
Each book object takes a title, optional author, and optional keywords.

"""
*class** Book*:
    *def*
* __init__*(self, title, author="Unknown", keywords=[]):
        """
        Books take three arguments to their constructor

        The first and only required argument is the title,
        followed by the optional arguments, author, and a list
        of keywords that can be used to look up a specific book.
        """

        self.title = title
        self.author = author
        self.keywords = keywords

    *def** setTitle*
(self, title):


This seems bizzare and redundant, but I did it anyway, all the time
thinking, "Why not just clone the variable,  I.E.
'titleForMyPurposesHereInThisInstance = title'? "

Does it follow that if one does not prepend "self" to the variable, any
change will affect all instances of an object (say in a threading
situation)? That is the only thing that seems to make sense to me, otherwise
"self" would just be silly.

I'd recommend going back and rethinking the design of your
> objects before going much further.


As I said, all that stuff was throwaway anyway and I should have made a more
generic example.

Thanks!


HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061026/363725cf/attachment.htm 


More information about the Tutor mailing list