[Tutor] Dicts and classes

VanL van@lindbergs.org
Tue, 12 Mar 2002 12:58:42 -0700


Hello,

If I understand correctly, a class is more or less a dict with a little 
special sauce thrown in.

Thus, can I assign to a class like I would a dict?  Get a list of members?

For example, this doesn't seem very pythonic:

class customer:
    def __init__(self, name, lastname, address, address2, email, phone, 
areacode, fax, faxarea .....):
         self.name = name
         self.lastname = lastname
         self.address = address
         self.address= address2
[snip]
    def changename(self, name): self.name = name
    def changelastname(self, name): self.lastname = name
[snip]
     def printname(self): print self.name


I have exaggerated here, but you get the idea.
Is there any way to do this:

class customer:
    def __init__(self, info):
        for k in info.keys(): self.k = info[k]
    def change(self, name, value):
        self.name = value
    def print(self, name):
        print self.name

and even:

bob = customer({'name':'Bob', 'lastname':'Jones','phone':'555-1234'})
for member in bob.keys(): print bob.member

or finally:
print 'Customer: %(name)s %(lastname)s' % bob