yes, with metaclasses (was Re: Read-only class definitions?)

sismex01 at hebmex.com sismex01 at hebmex.com
Fri Oct 11 09:26:15 EDT 2002


> From: Troels Therkelsen [mailto:t_therkelsen at hotmail.com]
> 
> In article <xxxp9.32771$Fz.941846 at news1.tin.it>, Alex Martelli wrote:
> > [...snip...nice metaclass example by Alex Martelli...]
> 
> Alex,
> 
> Thanks a lot for the pointer on the __metaclass__, I had 
> totally ignored that aspect.  This will be useful in another
> project I'm working on :-)
> 
> However, what would prevent the code from doing
> 
> del a.__metaclass__.__delattr__
> del a.afunc
> 
> ?
> 
> 
> Regards,
> 
> Troels Therkelsen
>

There, you seem to be trying to prevent malicious users from
mangling your classes; I don't think it can be done with
Python, since there's no artificial restrictions (a'la
"private" and "protected" class members) placed on the
language, but then, even in those that have such artificial
restrictions you can do that; just a reminder of how to
completely expose any hidden functionality on a C++ class:

#define protected public
#define private public
#include <header-with-class-to-rape.h>

So in fact, what you should do is document the expected
interfase and behaviour, and warn about mangling the object's
insides. It's up to the individual developer to behave in
a responsible manner when using it.

HTH

-gustavo













More information about the Python-list mailing list