metaclass and customization with parameters

Mike C. Fletcher mcfletch at rogers.com
Sun Oct 3 17:53:41 EDT 2004


zipher wrote:
...

>Yes, your suggestions were my second, and first implemenations
>respectively.  However, both require defining a class by the 'user',
>so to speak, as opposed to letting the user simply call a 'function'
>that returns the customized class with the filter class attribute
>already set.
>
>Another reason it would be helpful to be able to wrap this in an
>expression like "Metadict(filter=int)" is because I can then use this
>as a parameter passed to other parts of the application.  (Although,
>as you suggested, I'd have to pass the name as well.)
>  
>
I could *swear* I sent you an answer which did exactly what you wanted 
just a few minutes after your first posting (at least Mozilla claims I did):

def Metadict( name, **args ):
   return type( name, (baseClassWithCommonCode,), args )

That is, a function that you call with a name for the new class, and a 
set of parameters which will be passed into the namespace of the newly 
created class, (where the newly created class is to be a sub-class of 
some specified base class), which returns the new class.  This does, 
indeed, use a meta-class (type), btw, it just doesn't use the metaclass 
hook, which is what's bothering you elsewhere.

i.e.

    fred = Metadict( 'fred', filter=int )

should work fine with that function, and it seems to meet all of your 
other criteria modulo magically knowing the name to which it's being 
assigned.

Enjoy,
Mike

________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list