@classmethod question

M.-A. Lemburg mal at egenix.com
Thu Apr 24 05:34:30 EDT 2008


On 2008-04-24 05:27, Scott SA wrote:
> Hi,
> 
> I'm using the @classemethod decorator for some convenience methods and for some reason, either mental block or otherwise, can't seem to figure out how to elegantly detect if the call is from an instance or not.
> 
> Here's the problem: Within the class definition, 'isinstance' has nothing to compare to because the class does not appear to exist.
> 
> This is NOT a great example, but it outlines the the code:
> 
>     class RecipieClass:
>         def __init__(self):
>             pass
>         
>         @classmethod
>         def get_ingrendients(self, recipie_list=None):
>             
>             if isinstnace(self,RecipieClass):
>                 return self.do_something_interesting()
>             else:
>                 return do_something_boring(recipie_list)
> 
> Yes, I can test to see if the param exists, but that makes the call exclusive i.e. I can _only_ call it as an instance or with a parameter.

I would turn the above method into a regular instance method
and then add a new function or static method which then
returns the summary output.

A class method is clearly wrong here, since those are meant to
be called with the class as first argument, e.g. to count
the number of instances created from a class.

> Why am I doing this?
> 
> It is a series of convenience methods, in this case I'm interacting with a database via an ORM (object-relational model). I want the ability to call a class-ojbect and get related values, or pass some criteria and get related values for them without collecting the records first as instances, then iterating them. I need to call this from several places so I want to be DRY (don't repeat yourself).
> 
> The easiest way to describe this as an analogy would be like having a recipie for cookies and wanting to know all of the ingredients ahead of time. Then, at another time, wanting to know what all the ingredients would be to make cookies, cake and bread (i.e. complete shopping list).
>     
>   cookie_recipie = RecipieClass.get_recipie('cookies')
>   cookie_recipie.get_ingredients()
>         2C Flour
>         0.5 C Sugar
>         ...
>         
>   RecipieClass.get_ingrendients(['cookies','cake','bread'])
>         8C Flour
>         2C Sugar
>         ...
> 
> Of course any suggestions on how this might be better approached would be interesting too.
> 
> TIA,
> 
> Scott
> --
> http://mail.python.org/mailman/listinfo/python-list

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 24 2008)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
            Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the Python-list mailing list