Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'definitions': 0.07; 'python': 0.08; '__name__': 0.09; 'attribute': 0.09; 'either.': 0.09; 'instance.': 0.09; 'namespace': 0.09; 'question?': 0.09; 'to:name:python list': 0.09; 'error:': 0.12; "'__main__':": 0.16; 'attribute,': 0.16; 'bases,': 0.16; 'complains': 0.16; 'construct.': 0.16; 'dangerous,': 0.16; 'fruit': 0.16; 'grateful.': 0.16; 'hypothetical': 0.16; 'instances.': 0.16; 'metaclass': 0.16; 'subject:Trying': 0.16; 'def': 0.16; '(most': 0.21; 'loop': 0.22; "doesn't": 0.22; 'dictionary': 0.23; 'wrong?': 0.23; 'code': 0.24; 'traceback': 0.25; 'tried': 0.27; "i'm": 0.27; 'raise': 0.28; 'attach': 0.28; '27,': 0.29; 'mapping': 0.29; 'object': 0.30; 'example': 0.30; 'key,': 0.30; 'value)': 0.30; 'zero.': 0.30; 'class': 0.31; 'this.': 0.31; 'print': 0.32; 'does': 0.32; 'it.': 0.33; 'to:addr:python-list': 0.34; 'header :User-Agent:1': 0.34; 'someone': 0.34; 'quite': 0.34; 'things': 0.34; 'none': 0.35; 'starting': 0.35; 'last):': 0.35; 'running': 0.35; 'skip:" 10': 0.36; 'file': 0.36; 'page': 0.36; 'doing': 0.37; 'but': 0.37; 'url:org': 0.38; 'something': 0.38; 'think': 0.38; 'run': 0.39; 'happened': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'where': 0.40; 'your': 0.60; 'happen': 0.62; 'divided': 0.73; 'god': 0.76; 'age': 0.83; "'person'": 0.84; "driver's": 0.84; 'flies': 0.84; 'organ': 0.84; 'stranger': 0.84; 'subject:learn': 0.84; 'value):': 0.84; 'holes': 0.91; '000': 0.93 Date: Mon, 25 Jul 2011 11:36:31 -0400 From: "Steven W. Orr" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: python list Subject: Trying to learn about metaclasses Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311608302 news.xs4all.nl 23921 [2001:888:2000:d::a6]:37984 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10282 I have been doing a lot of reading. I'm starting to get it. I think it's really cool as well as dangerous, but I plan on being respectful of the construct. I found a web page that I found quite readable. http://cleverdevil.org/computing/78/ So, I tried to run the example code (below), and I get AttributeError. If someone can get me over this hump, I'd be grateful. It complains that Person has no _fields attribute, but the print hello I have in EnforcerMeta doesn't happen either. Am I doing something wrong? I'm running python 2.6.2 Here's the error: 585 > ./meta1.py Traceback (most recent call last): File "./meta1.py", line 38, in swo.name = 'swo' File "./meta1.py", line 27, in __setattr__ if key in self._fields: AttributeError: 'Person' object has no attribute '_fields' And here's the code: #! /usr/bin/python # http://cleverdevil.org/computing/78/ class Field(object): def __init__(self, ftype): self.ftype = ftype def is_valid(self, value): return isinstance(value, self.ftype) class EnforcerMeta(type): def __init(cls, name, bases, ns): # Store the field definitions on the class as a dictionary # mapping the field name to the Field instance. print 'Hello' cls._fields = {} # loop through the namespace looking for Field instances. for key, value in ns.items(): if isinstance(value, Field): cls._fields[key] = value class Enforcer(object): # attach the metaclass __metaclass__ = EnforcerMeta def __setattr__(self, key, value): if key in self._fields: if not self._fields[key].is_valid(value): raise TypeError('Invalid type for field.') super(Enforcer, self).__setattr__(key, value) class Person(Enforcer): name = Field(str) age = Field(int) if __name__ == '__main__': swo = Person() swo.name = 'swo' print 'swo:', swo -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net