Hi all, I've been sick, and have not had the strength to continue last week's thread on slots. I'll get back to it, but in the mean time I thought I'd share a little of what my feverish brain has come up with: First, should we add the type of a slot descriptor to 'types'? i.e.: # Get the type of a member descriptor (its not currently in types) class _Foo(object): __slots__ = 'a' member_descriptor = type(_Foo.a) del _Foo If so, I'll submit a patch related the the above. Second, I've been twisting metaclasses to do my evil bidding. In this case, I have implemented pseudo-typed attributes and slots in Python. e.g.: class A(object): __metaclass__ = ConstrainedObject __slots__ = {'a':int,'b':float} class B(A): __attrs__ = {'c':str} # My own invention foo=B() foo.a = 1 foo.b = 1 foo.c = 1 print foo.a,foo.b,foo.c
1 1.0 1
print type(foo.a),type(foo.b),type(foo.c)
foo.a = '5' foo.b = '0x5A' foo.c = 5.4 print foo.a,foo.b,foo.c
5 1.40625 5.4
foo.a = 'Spam!'
ValueError: invalid literal for int(): Spam!
The first thing to notice is that float('0x5A') == 1.40625?! I can see what it is doing, though it doesn't seem to be the most intuitive behavior. However, this is tangential to what I really care about -- I just thought I'd share my initial confusion. Thirdly, properties doesn't seem to be subclassable. Or at least they fail to be properties once subclassed. This isn't a big deal, but it should be documented if it is an intentional behavior. See the attached file for more why... Third, metaclasses are _fun_. The above example uses silly constraint objects, since they do all kinds of possibly smart and annoying things. e.g., Instead of using str, int and float, we can easily define functions like: def str_constraint(s): if type(s) is not types.StringType: raise TypeError, "I'm sorry Dave, but I expected a string." return s Anyhow, the implementation of my constraint meta-class is attached. Give it a read-over if you feel masochistic and let me know what you think. Still running a fever, -Kevin -- -- Kevin Jacobs The OPAL Group - Enterprise Systems Architect Voice: (216) 986-0710 x 19 E-mail: jacobs@theopalgroup.com Fax: (216) 986-0714 WWW: http://www.theopalgroup.com