metaclass ???

Mike C. Fletcher mcfletch at rogers.com
Wed Aug 18 22:36:17 EDT 2004


BruceKL WhoH wrote:
...

>    What I'm trying to do is :
>    I want to make two class, they are just subtly different,for example
>  
>
...

>all other codes are identical,just f is different.
>    So can I use meta-class here? Or I should do this by another way?
>  
>
If this is the entire specification of what you need to do, it really
seems as though you'd be better off with mix-in classes. Your code would
then look like this:

class Base:
"""Base class providing common functionality"""
# your shared code here

class A( Base ):
"""Overrides method f only"""
def f(self):
print 'a'

class B( Base ):
"""Overrides method f only"""
def f(self):
print 'b'

The first page of the presentation is intended to warn you that, in
general, most programmers never need to use metaclasses, even after
years of work in the language. The use cases for them are fairly exotic,
not the kind of thing you find in "normal" programming operations.

There are cases where you might want to use metaclasses to inject a
method into a class (as covered in the presentation), but those
use-cases tend to be more involved than simply wanting to have 1 method
change. Those cases are normally going to involve making a *decision* at
run-time as to which method implementation to include, for instance
based on the presence/absence of other methods or a data-attribute of
the class.

>    BTW,your pdf is quite nice,how do you make it? Beamer,pdfscreen, or PPT ?
>  
>
OpenOffice.org Presenter (PowerPoint-like app) has a built-in export to PDF.

Have fun,
Mike

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




More information about the Python-list mailing list