[Tutor] Dicts and classes

Sheila King sheila@thinkspot.net
Tue, 12 Mar 2002 15:22:46 -0800


On Tue, 12 Mar 2002 12:58:42 -0700, VanL <van@lindbergs.org>  wrote about
[Tutor] Dicts and classes:

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

Well, a class needn't necessarily have anything to do with a dict, although
it may.

> 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
> 

You don't have to write a function to change the name. You can just do
this:

>>> class customer:
	def __init__(self, name, lastname, address, address2, areacode):
		self.name = name
		self.lastname = lastname
		self.address = address
		self.address2 = address2
		self.areacode = areacode

		
>>> bob = customer('bob', 'smith', '1122 boulevard st', 'springfield, GA', '320')
>>> bob.areacode
'320'
>>> bob.areacode = '321'
>>> bob.areacode
'321'
>>> 

So you don't have to write a function for changing each of these values.
Just change it by an assignment statement.

As for printing...

You don't necessarily have to write a function to handle this. You can just
print out the separate values, like this:

>>> print bob.name
bob
>>> 

However, it might be nice to have a way of asking a customer to print
itself, and have it come out nicely formatted. What about this:

class customer:
    def __init__(self, name, lastname, address, address2, areacode):
        self.name = name
        self.lastname = lastname
        self.address = address
        self.address2 = address2
        self.areacode = areacode

    def __str__(self):
        mystring = "%s,%s\n%s\n%s\n%s" % \
            (self.lastname, self.name, self.address,
             self.address2, self.areacode)
        return mystring


bob = customer('Bob', 'Smith', '1122 Boulevard St.', 'Springfield, GA',\
	 '320')
print bob



When I run the above code, I get:

Smith,Bob
1122 Boulevard St.
Springfield, GA
320

> 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


How about this? (Although I'm not sure I prefer it to the first version
that doesn't use dicts)

class customer:
    def __init__(self, infodict):
        self.info = infodict

    def __str__(self):
        mystring = "%s,%s\n%s\n%s\n%s" % \
            (self.info['lastname'], self.info['name'],
             self.info['address'],
             self.info['address2'], self.info['areacode'])
        return mystring


bob_dict = {'name':'Bob', 'lastname':'Smith',
       'address': '1122 Boulevard St.',
       'address2': 'Springfield, GA',
       'areacode': '302'}

bob = customer(bob_dict)
print bob


When I run it, I get the same output as I showed in the previous version:

Smith,Bob
1122 Boulevard St.
Springfield, GA
302


Does this help any?

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/