473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

@classmethod question

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 howto 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.

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 aclass-ojbect and get related values, or pass some criteria and get relatedvalues 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
Jun 27 '08 #1
3 1235
On 24 ÁÐÒ, 07:27, Scott SA <py...@rscorp.ab.cawrote:
Hi,

I'm using the @classemethod decorator for some convenience methods and forsome reason, either mental block or otherwise, can't seem to figure out howto 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.

Why am I doing this?

It is a series of convenience methods, in this case I'm interacting with adatabase via an ORM (object-relational model). I want the ability to call aclass-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
Hi,
It would make sense to separate instance-level and class-level
behaviour with additional 'objects' namespace. e.g.
cookie_recipie.get_ingredients() to get ingredients only for cookie
recipie and RecipieClass.objects.get_ingrendients([....]) to get all
the ingredients.

The elegant solution (AFAIK used by Django) would be to use metaclass
and the object with custom descriptor for class-object/table level
stuff.

Something like this:

class RecipieMetaclass(type):
def __new__(cls, bases, attrs):
new_cls = type.__new__(cls, name, bases, attrs)
new_cls.objects = IngredientsDescriptor(IngredientsManager())
return new_cls

class RecipieClass(object):
__metaclass__ = RecipieMetaclass
def get_ingredients(self, recipie_list=None):
return self.do_something_interesting(recipie_list)

class IngredientsManager(object):
def get_ingredients(self, recipie_list=None):
return do_something_boring(recipie_list)

class IngredientsDescriptor(object):
def __init__(self, ingredients_manager):
self.ingredients_manager = ingredients_manager
def __get__(self, instance, type=None):
if instance is not None:
raise AttributeError, "Access via %s instances is not
allowed" % type.__name__
return self.ingredients_manager

Then, "at another time, wanting to know what all the ingredients would
be to make cookies, cake and bread" you would call:
RecipieClass.objects.get_ingrendients(['cookies','cake','bread'])

Both Django and Google Apps Engine API use similar concepts and you
can learn much more interesting looking in their source code.

Regards,

--
Ivan
Jun 27 '08 #2
Scott SA a écrit :
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.
Well, the point is that a classmethod *always* receive the class as
first argument, wether it's called on the class or an instance. If
that's not what you want, then you don't use the right tool.

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.

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).
out of curiosity : which one ?
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.

Why do you want the same method to do two different things ? You clearly
have two distincts methods doing different things here, and as a user of
your code I'd find your API confusing. May I suggest a much simpler
approach:
class Recipies(object):
@property
def ingredients(self):
return <dict of ingredient:qty for self>

@classmethod
def get_ingredients_for(cls, *id_recipies):
return <dict of ingredient:summed_qty for all id_recipies>
print Recipie.get('cookie').ingredients
print Recipies.get_ingredients_for('cookie', 'cake', 'bread')

My 2 cents...
Jun 27 '08 #3
Scott SA <py***@rscorp.ab.cawrites:

A side note
class RecipieClass:
Recipe is a more widespread spelling, I believe. Moreover it is the
convention in python that only class names are capitalized, so you
don't need to append a 'Class'.

class Recipe:
...
clafoutis = Recipe('eggs', 'spam')

--
Arnaud
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zak Arntson | last post by:
I'm currently implementing a game GUI, where there are three components to the screen: A battle, a command button area and a city view area. Each of these are rectangular, and handle interactions...
5
by: C Gillespie | last post by:
Hi, Does anyone know of any examples on how (& where) to use staticmethods and classmethods? Thanks Colin
3
by: Giovanni Bajo | last post by:
Hello, what's the magic needed to reuse the base-class implementation of a classmethod? class A(object): @classmethod def foo(cls, a,b): # do something pass
6
by: Laszlo Zsolt Nagy | last post by:
Hello, Is it possible to tell, which instance was used to call the classmethod that is currently running? Background: I have a class called DatabaseConnection and it has a classmethod called...
3
by: andychambers2002 | last post by:
Hi, I'm trying to write a method that needs to know both the class name and the instance details class A: @classmethod def meth(cls, self): print cls
2
by: Sylvain Ferriol | last post by:
hello can you explain why python does not see difference between instance method and class method, having the same name example .... def f(self): .... print('instance method')...
14
by: james_027 | last post by:
hi, python's staticmethod is the equivalent of java staticmethod right? with classmethod, I can call the method without the need for creating an instance right? since the difference between...
3
by: Matthew Keene | last post by:
I would like to be able to call a specific classmethod on a class name that is going to be passed from another parameter. In other words, I have a call that looks something like: x = Foo.bar()...
0
by: Gabriel Genellina | last post by:
En Thu, 24 Apr 2008 00:27:13 -0300, Scott SA <pydev@rscorp.ab.caescribió: Then you can't use a classmethod. A class method can *only* be called on the defining class or a subclass of it, or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.