[Python-Dev] Explorations on new-style classes
Kevin Jacobs
jacobs@penguin.theopalgroup.com
Tue, 5 Mar 2002 15:23:07 -0500 (EST)
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Send mail to mime@docserver.cac.washington.edu for more info.
--416156269-816924027-1015359787=:5551
Content-Type: TEXT/PLAIN; charset=US-ASCII
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)
> <type 'int'> <type 'float'> <type 'str'>
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
--416156269-816924027-1015359787=:5551
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="constraints.py"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.44.0203051523070.5551@penguin.theopalgroup.com>
Content-Description:
Content-Disposition: attachment; filename="constraints.py"
IyBHZXQgdGhlIHR5cGUgb2YgYSBtZW1iZXIgZGVzY3JpcHRvciAoaXRzIG5v
dCBjdXJyZW50bHkgaW4gdHlwZXMpDQpjbGFzcyBfRm9vKG9iamVjdCk6DQog
IF9fc2xvdHNfXyA9ICdhJw0KbWVtYmVyX2Rlc2NyaXB0b3IgPSB0eXBlKF9G
b28uYSkNCmRlbCBfRm9vDQoNCiMgVGhpcyBjdXJyZW50bHkgZG9lcyBub3Qg
d29yayANCiMgLS0gcHJvcGVydHkgaXMgbm90IGN1cnJlbnRseSBzdWItY2xh
c3NhYmxlLCBzaW5jZSB0aGUgcmVzdWx0aW5nIG9iamVjdHMgbm8NCiMgbG9u
Z2VyIGFjdCBsaWtlIHByb3BlcnRpZXMuDQpjbGFzcyBjb25zdHJhaW50X2Rl
c2NyaXB0b3IocHJvcGVydHkpOg0KICBkZWYgX19uZXdfXyhjbHMsIG5hbWUs
IGNvbnN0cmFpbnQsIGJhc2VfZGVzY3IgPSBOb25lKToNCg0KICAgICMgQnVp
bGQgYSBkZXNjcmlwdG9yIGZvciBhbiBpbnN0YW5jZSBhdHRyaWJ1dGUgDQog
ICAgaWYgbm90IGJhc2VfZGVzY3I6DQogICAgICBmZ2V0ID0gbGFtYmRhIHNl
bGYseD1uYW1lOiB2YXJzKHNlbGYpW3hdDQogICAgICBmc2V0ID0gbGFtYmRh
IHNlbGYsdmFsLHg9bmFtZSx0Yz1jb25zdHJhaW50OiB2YXJzKHNlbGYpLl9f
c2V0a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