[Tutor] class data

alan.gauld@bt.com alan.gauld@bt.com
Mon, 3 Dec 2001 13:55:48 -0000


I'll have a go at what I think is confusing you...

> A class is like a Python container type on steroids.  
> Not only can it hold multiple data items but it can 
> also support its own set of functions,
> ...
> Objects contained in lists are unrelated except for the name of their
> container.  Its members are accessed only via an index offset into an
> array-like data structure.  All lists have a common set of methods and
> provide key access to their members (who are also unrelated except for
> their container name).
> ...
> 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.  

> So if class data is not stored in standard containers, where 
> is it stored

In the class. The class is a type of container in its own right.

You can however save instances of the class(ie objects) in 
standard containers.

> I'm talking about the names, phone numbers, 
> e-mail addresses, etc. in the AddrBookEntry class example.)

class Entry:
   def __init__(s, nm,ph,em):
      s.name = nm
      s.phone = ph
      s.email = em
   def saveme(s,flnm):
      f = open(flnm,'a')
      f.write(`s.name`,'\t',`s.phone`,'\t',s.email`)

# We now have a class container which holds two methods

book = []
for i in range(10):
   book.append(Entry('al','1234','ag.co.com')

# we now have 10 instances stored in a standard list container

for e in book:
   e.saveme('book.txt')

# we now save the instance data from all 10 instances

Does that help?

Alan g.