Using eval, or something like it...

Scott David Daniels Scott.Daniels at Acm.Org
Fri Nov 21 13:43:01 EST 2008


I forgot to include a few cases:

(1) Inspired by your calling the class attributes "templates":
     class Demo3(object):
         pass

     d = Demo3()
     print d.non_template  # raises exception
     d.non_template = 45
     print d.non_template
     print Demo3.non_template  # raises exception
     Demo3.non_template = 44
     print d.non_template
     print Demo3.non_template  # raises exception
     del d.non_template
     print d.non_template
     del d.non_template  # raises exception
     print d.non_template
     del Demo3.non_template
     print d.non_template  # raises exception

All the above to demonstrate the class storage and instance storage
are separate.

(2) Trickier stuff involving sharing:
     class Demo4(object):
         common = []

     g = Demo4()
     h = Demo4()
     g.common.append('a')
     print g.common, h.common, Demo4.common
     g.common = g.common
     Demo4.common = Demo4.common + ['b']
     h.common = h.common
     x = Demo4()
     g.common.append('c')
     h.common.append('d')
     x.common.append('e')
     print g.common, h.common, Demo4.common, x.common

> Thanks for your reply and examples BTW (I think) they are helping me
> clarify my understanding of python classes and the language used to
> describe them.

They are meant to point out that you need to read language definitions
carefully, or you will breeze by thinking you understand something you
really do not understand.  Python provides a great way to experiment
with things you think you get; try "corner cases" to make sure you
know what is going on.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list