I have a class factory problem. You could say that my main problem is
that I don't understand class factories.
My specific problem:
I have a class with several methods I've defined.
To this class I want to add a number of other class methods where the
method names are taken from a list.
For each list member, I want to build a method having the list member
name so that I can override another method of the same name for objects
which are instances of this class. Finally, I want my class factory
which creates the method to call the overridden method.
I'm sure my description is confusing, so I'll try to illustrate it.
import Numeric
# The Numeric module contains a whole lot of methods which operate on
certain number types
class foo:
""" this is a number type class
"""
def __init__(self, value):
self.value = float(value)
def __str__(self): 5 1703
Thanks for the very helpful reply Rainer,
I thought I couldn't use setattr because I need the syntactic sugar
sqrt(f) to work, but with your example code Numeric.sqrt(f) does in fact
work correctly. I also need to do a bit more than may be possible with a
simple lambda function, but I should be able to sort it out from here
with the info you provided,
thanks again,
Gary
Rainer Mansfeld wrote:
<snip> Hi Gary,
you want your 'class factory' to change the methods of Numeric, so that they accept foo objects and return foo objects? I've not the slightest idea how to achieve that.
If OTOH you want your foo class to have sqrt, arccos, etc. methods without defining them explicitly, I think you're looking for something like:
. import Numeric . . class Foo(object): . def __init__(self, value): . self.value = float(value) . for u in ['sqrt', 'cos', 'tan']: . setattr(self, u, lambda uf=getattr(Numeric, u): . uf(self.value + 42.0))
>>> f = Foo(7) >>> f.sqrt()
7.0
HTH Rainer
OK, I've managed to get this to work with Rainer's method, but I
realised it is not the best way to do it, since the methods are being
added by the constructor, i.e. they are instance methods. This means
that every time a foo object is created, a whole lot of code is being
run. It would be better to do the same thing with class 'static'
methods, if this is possible, so that the methods are created just once.
Is this possible?
Gary
Rainer Mansfeld wrote:
<snip> If OTOH you want your foo class to have sqrt, arccos, etc. methods without defining them explicitly, I think you're looking for something like:
. import Numeric . . class Foo(object): . def __init__(self, value): . self.value = float(value) . for u in ['sqrt', 'cos', 'tan']: . setattr(self, u, lambda uf=getattr(Numeric, u): . uf(self.value + 42.0))
>>> f = Foo(7) >>> f.sqrt()
7.0
HTH Rainer
Gary Ruben wrote: OK, I've managed to get this to work with Rainer's method, but I realised it is not the best way to do it, since the methods are being added by the constructor, i.e. they are instance methods. This means that every time a foo object is created, a whole lot of code is being run. It would be better to do the same thing with class 'static' methods, if this is possible, so that the methods are created just once. Is this possible?
See my comment on the recipe at: http://aspn.activestate.com/ASPN/Coo.../Recipe/389793
STeVe
Gary Ruben wrote: OK, I've managed to get this to work with Rainer's method, but I realised it is not the best way to do it, since the methods are being added by the constructor, i.e. they are instance methods. This means that every time a foo object is created, a whole lot of code is being run. It would be better to do the same thing with class 'static' methods, if this is possible, so that the methods are created just once. Is this possible?
Here is a solution using a metaclass:
import Numeric
class MetaFoo(type):
def __init__(cls, name, bases, d):
super(MetaFoo, cls).__init__(cls, name, bases, d)
for u in ['sqrt', 'cos', 'tan']:
setattr(cls, u, lambda self, uf=getattr(Numeric, u): uf(self.value + 42.0))
class Foo(object):
__metaclass__ = MetaFoo
def __init__(self, value):
self.value = float(value)
f = Foo(7)
print f.sqrt()
Kent
Thanks Steven and Kent, both of your suggestions look good to me. I'll
try both out and pick one.
Gary
Gary Ruben wrote: OK, I've managed to get this to work with Rainer's method, but I realised it is not the best way to do it, since the methods are being added by the constructor, i.e. they are instance methods. This means that every time a foo object is created, a whole lot of code is being run. It would be better to do the same thing with class 'static' methods, if this is possible, so that the methods are created just once. Is this possible? Gary This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Carlos Ribeiro |
last post by:
I thought about this problem over the weekend, after long hours of
hacking some metaclasses to allow me to express some real case data
structures as Python classes. I think that this is something...
|
by: Johan Bergman |
last post by:
Hi,
Maybe someone can help me with this one. The following describes a
somewhat simplified version of my problem, but I think it will be
sufficient.
I want to use class factories (virtual...
|
by: Aguilar, James |
last post by:
My previous example used the concept of a Shape class heirarchy, so I will
continue with that.
Suppose I have something like fifty different shapes, and I am trying to
instantiate one of them. ...
|
by: Patrick Stinson |
last post by:
I am trying to create a way to register static members of a **class**, not
an object, for making an object factory. The main thing I want is to be
able to make a call like
class MyClass
{...
|
by: Jon Rea |
last post by:
I hav been looking for the last 2 hours on how to do this without much luck.
Im going to give a simplifed model of the problem i have.
I want a collection class that can holds a series or...
|
by: Craig Buchanan |
last post by:
I've seen design patterns for class factories that work well to create
(fetch) objects, but I haven't seen anything about how to persist the class'
data when it has changed. Is this done thru the...
|
by: deko |
last post by:
Which layer should a Factory class go in?
DA - Data Access
BL - Business Logic
UI - User Interface
??
|
by: docschnipp |
last post by:
Hi,
I have a bunch of object derived from the same base class. They all share
the same constructor with some parameters. Now, instead of using a large
switch() statement where I call every...
|
by: =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?= |
last post by:
Morning,
I've got an ASP.NET 2.0 Web Application. Behind it, I've a
statically-scoped facade/class factory as the business layer, running atomic
functions back and forth from my by DAL.
The...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |