[Tutor] class data

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 2 Dec 2001 14:17:35 -0800 (PST)


On Sun, 2 Dec 2001 fleet@teachout.org wrote:

> "In this section, we will take a close look at classes and what types
> of attributes they have.  Just remember to keep in mind that even
> though classes are objects (everything in Python is an object), they
> are not realizations of the objects they are defining. We will look at
> instances in the next chapter, so stay tuned for that.  For now, the
> limelight is strictly beamed on class objects."

I think the author is making the distinction between the class
'AddrBookEntry':

##
>>> AddrBookEntry
<class __main__.AddrBookEntry at 0x8119034>
##

and an instance of that class:

###
>>> a = AddrBookEntry('John Doe', '405-555-1212')
>>> a
<__main__.AddrBookEntry instance at 0x8110e5c>
###



> "When you create a class, you are practically creating your own kind
> of data entity.  All instances of that class are similar, but classes
> differ from each other (and so will instances of different classes by
> nature). Rather than playing with toys that come from the manufacturer
> and were bestowed upon you as gifts, why not design and build your own
> toys to play with?"


> So if class data is not stored in standard containers, where is it
> stored (and how)?  (I'm talking about the names, phone numbers, e-mail
> addresses, etc. in the AddrBookEntry class example.)

>From what I remember, instance data actually is stored in a standard
dictionary container:

###
>>> a = AddrBookEntry('John Doe', '405-555-1212')
Created instance for: John Doe
>>> a.__dict__
{'phone': '405-555-1212', 'name': 'John Doe'}
###

Python usually hides this dictionary so that, in casual use, we never
really need to worry about it --- we can just access the 'properties' of
our class with the special Python notation:

###
>>> a.phone, a.name
('405-555-1212', 'John Doe')
###


Still, nothing stops us from saying:

##
>>> a.__dict__['phone'], a.__dict__['name']
('405-555-1212', 'John Doe')
###

It's just uglier that way.


Hope this helps!