Is this an ok thing to do in a class

Ethan Furman ethan at stoneleaf.us
Tue May 18 16:41:59 EDT 2010


Vincent Davis wrote:
> Just wondering if there is a problem with mixing a dictionary into a 
> class like this. Everything seems to work as I would expect.
> 
> class foo(object):
>     def __init__(self, x):
>         self.letter = dict(a=1,b=2,c=3)
>         self.A=self.letter['a']
>         self.x=self.letter[x]
> 
>  >>> afoo = foo('b')
>  >>> afoo.x
> 2
>  >>> afoo.A
> 1
>  >>> afoo.letter['a']
> 1
>  >>> afoo.letter.items()
> [('a', 1), ('c', 3), ('b', 2)]

Do you expect afoo.letter[x] to always be afoo.x?  Because they aren't:

 >>> afoo.A = 9
 >>> afoo.letter['a']
1

~Ethan~



More information about the Python-list mailing list