[Tutor] Simple Question On A Method (in subclass)

Marc Tompkins marc.tompkins at gmail.com
Tue Oct 25 19:47:22 CEST 2011


On Tue, Oct 25, 2011 at 5:31 AM, Chris Kavanagh <ckava1 at msn.com> wrote:

>
>
> On 10/25/2011 3:50 AM, Dave Angel wrote:
>
>> On 10/25/2011 12:20 AM, Chris Kavanagh wrote:
>>
>>>
>>>
>>> On 10/24/2011 12:06 AM, Marc Tompkins wrote:
>>>
>>>> On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh <ckava1 at msn.com
>>>> <SNIP>
>>>>
>>>
>>> My problem was, I wasn't seeing {member} as referring to the class
>>> objects {t} and {s}. Since it was, we now can use member just like any
>>> class object, and combine it with class functions (and class
>>> variables), such as {member.tell}. I had never in my short programming
>>> experience, seen an example like this. So I was confused, obviously, LOL.
>>>
>>>
>> In the context of:
>>
>> t = Teacher('Mrs. Shrividya', 40, 30000)
>> s = Student('Swaroop', 22, 75)
>> members = [t, s]
>>
>> for member in members;
>> member.dosomething()
>>
>> member does not refer to t and s at all. It refers to the same object as
>> t and as s, in succession. members is a list of references to objects.
>> Each item in members is bound to a particular object. It is not bound in
>> any way to s or t.
>>
>> For example, suppose we did:
>>
>> members = [t, s]
>> t = 42
>> for member in members:
>> member.dosomething()
>>
>> member still references the object holding Mrs. Shrividya, or Swaroop,
>> in succession, even though t is now (bound to) an integer (object).
>>
>>
>>  I understand. . .Thanks for clearing that up for me Dave.
> So much to learn, so little time! LOL.
> Thanks again to everyone, it's much appreciated.
>

It can be a little hard to wrap your head around how Python handles
variables/objects; in other languages you create a variable and assign a
value to it, while in Python you create an object and assign a name to it -
the name can change while the object remains unchanged.  Here's a very
simplified demo of what Dave is talking about:

>>> t = "This"
>>> s = "That"
>>> members = [t, s]
>>> print members
['This', 'That']
>>> s = "The other"
>>> print members
['This', 'That']


In other words, "members" is NOT a list of "t" and "s" - it's a list of the
objects that "t" and "s" pointed to AT THE MOMENT you created "members".
You can re-assign "t" and "s" to other objects, but the objects that are
members of "members" remain unchanged until you manipulate them directly:

>>> members[0] = "Something else entirely"
>>> print members
['Something else entirely', 'That']

>>> print t, s
This The other

It's extremely logical, but almost entirely backward from the way most other
languages do things.  Possibly it's because Guido is Dutch.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111025/b68f5a19/attachment.html>


More information about the Tutor mailing list