instance + classmethod question

Mike Meyer mwm at mired.org
Mon Dec 12 21:08:44 EST 2005


Laszlo Zsolt Nagy <gandalf at designaproduct.biz> writes:
> Mike Meyer wrote:
>>Laszlo Zsolt Nagy <gandalf at designaproduct.biz> writes:
>>>Is it possible to tell, which instance was used to call the
>>>classmethod that is currently running?
>>Ok, I read through what got to my nntp server, and I'm still
>>completely confused.
>>A class method isn't necessarilry called by an instance. That's why
>>it's a class method. What should happen in that case?
> Here is the answer (an example):
>
>     @ClassOrInstanceMethod
>     def process_create_table(cls_or_self,tabledef,processor):
>         """Process the CREATE TABLE command.
>
>     @param tabledef: a L{TableDefinition} instance.
>     @param processor: a L{SQLProcessor} instance."""
>         hname = cls_or_self.hashident(tabledef.name)
>         if (isinstance(cls_or_self,type)) or (not
>         cls_or_self.istableexists(hname)):
>             processor.addline("create table %s \n ("% hname)
>             for field in tabledef.fields:
>                 if not (field() is None):
>                     cls_or_self.process_create_table_field(field(),processor)
>                     processor.addline(",")
>             processor.truncate_last_comma()
>             processor.addline("")
>             processor.addline(")")
>             cls_or_self.addtablespaceclause(tabledef,processor)
>             processor.processbuffer()

I assume that hashident, istableexists, process_create_table_field and
addtablespaceclause all do this screwy ClassOrInstanceMethod thing?

> There are two reasons why I do not want to create two methods (one
> instance and one class method).
>
> 1. If you look the above pattern, it is clear that the method does the
> same thing, just there are some conditions when I call it with an
> instance. I do not want to call "process_create_table_with_class" and
> "process_create_table_with_instance", because the name of the method
> should reflect what it does primarily. (BTW, I also looked at
> multimethods, but they are not exactly for this kind of problem.)

Well, I'd call them process_create_table_for_instance and
create_table_for_class, but that's just me.

> 2. The pattern above makes it clear that I just can't easily split the
> method into elementary parts. Steven wrote this pattern:

I didn't say it would be easy - just well understood, and on seeing
this, maybe not that. On the other hand, being hard doesn't mean it's
not worth doing.

> But I cannot do this, because primarily I do class stuff, and in some
> cases, I can make use of an instance (but do not require it).
> Comments welcome

Proposing an alternative design is pretty much impossible, because I
don't know how the class hierarchy fits together. If there isn't much
of a hierarchy, maybe you'd be better off with an ADT model than an
object model?

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list