473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stuck with metaclass

Hi,

I have a class Preconditions that has a metaclass (MetaChecker), but I guess I
somehow screwed things up with the metaclass definition...

When I create an instance of Preconditions, I get the doc string instead! =:-O
If I remove the metaclass statemente, everythign works fine.

Any help would be greatly appreciated. O:-)

here's my code:

--------------------------------------------------------------------------------------------------
import inspect

def _isNary(fn,n):
"""
Determines a function has the right 'arity'
"""

args = inspect.getargspec(fn)[0]
if len(args) == n:
return 1
else:
return None

class MetaChecker(type):
"""
Metaclas that checks if all 'public' methods' (those that
don't start with _ are unary functions
"""

def __new__(cls, clsname, bases, attribs):
for name, value in attribs.iteritems():
if name[0:1] != "_" and inspect.isfunction(value):
if not _isNary(value, 1):
try:
src = inspect.getsource(value)
except:
src = None

msg = "%s.%s is not a thunk method!"%(clsname,name)
if src:
msg += "\nOffending source:\n\n%s"%src

raise msg
return type.__new__(cls, name, bases, attribs)
class Preconditions (object):
"""
Can only have thunks.
"""

__metaclass__ = MetaChecker
pass

p = Preconditions()
print p
---------------------------------------------------------------

The result is:
<__main__.__doc__ object at 0x0093E970>

What the heck????????
Jul 18 '05 #1
1 1319
On Sat, 22 Nov 2003 18:50:24 +0100, Fernando Rodriguez <fr*@easyjob.net> wrote:
Hi,

I have a class Preconditions that has a metaclass (MetaChecker), but I guess I
somehow screwed things up with the metaclass definition...

When I create an instance of Preconditions, I get the doc string instead! =:-O
If I remove the metaclass statemente, everythign works fine.

Any help would be greatly appreciated. O:-)

here's my code:
Two suggestions:
For for a more generally useful return value
args = inspect.getargspec(fn)[0] return len(args) == n
and to fix the misleading symptom you observed
return type.__new__(cls, name, bases, attribs)

return type.__new__(cls, clsname, bases, attribs)

(No guarantee that's the end of it ;-)

Regards,
Bengt Richter
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
2
by: zipher | last post by:
After searching through comp.lang.python and the web regarding metaclasses, I could not find an example for customing classes using metaclass parameters. I want to be able to create a class at...
16
by: ironfroggy | last post by:
Hoping this isn't seeming too confusing, but I need to create a metaclass and a class using that metaclass, such that one of the bases of the metaclass is the class created with that metaclass. I...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
2
by: lbolognini | last post by:
Hi all, I dare risk my brain exploding by reaching for the understanding of metaclasses. At first i thought i almost got them, even if vaguely back in a corner of my mind, my understanding...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.