473,326 Members | 2,680 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,326 software developers and data experts.

dynamic

Hi all.
It's easier if I show an example first.

Say I have

class A(object):
def speak(self): print 'A!'

class B(object):
def speak(self): print 'B!'

I also have a factory function like this.

def foo(kind,*args,**kwds):
if kind=='a': return A(*args,**kwds)
else: return B(*args,**kwds)

I need foo to be a class, so that I could inherit from it and still use
it as a factory, so that I can do:

Foo('a').speak()
Foo('b'.speak()

class Final(Foo):
def __init__(self,kind,*args,**kwds):
super(Foo,self).__init__(kind,*args,*kwds)

Can I do it? How ?
If it is possible, I'm pretty sure it involves using __new__ on Foo, but I
can't figure out how to make it works.

Any help is appreciated.

Thank you,
Riccardo

--
Riccardo Galli
Sideralis Programs
http://www.sideralis.net
Jul 19 '05 #1
11 1322
Having a class that returns instances
of some other class is horrible, but
since you asked for it:

class A(object): pass
class B(object): pass

class Foo(object):
def __new__(cls, arg):
if arg=="a":
return A()
else:
return B()

print Foo("a")
print Foo("b")

Michele Simionato

P.S. don't do it!

Jul 19 '05 #2
On Wed, 15 Jun 2005 03:12:07 -0700, Michele Simionato wrote:
Having a class that returns instances of some other class is horrible, but
since you asked for it:

class A(object): pass
class B(object): pass

class Foo(object):
def __new__(cls, arg):
if arg=="a":
return A()
else:
return B()

print Foo("a")
print Foo("b")

Michele Simionato

P.S. don't do it!


Ciao Michele.

I have n classes wich share the same interface.
I then have a single class which add functionality to all the n classes,
using their interface.
The only way I see to make this single class inherith from the choosed nx
class is this one.

If there is a better approach, I can implement it.

Thank you for the answer,
Riccardo

--
Riccardo Galli
Sideralis Programs
http://www.sideralis.net
Jul 19 '05 #3
On Wed, 15 Jun 2005 13:13:49 +0200,
Riccardo Galli <riccardo_cut1@cut2_sideralis.net> wrote:
I have n classes wich share the same interface. I then have a single
class which add functionality to all the n classes, using their
interface. The only way I see to make this single class inherith from
the choosed nx class is this one. If there is a better approach, I can implement it.


class single(object):
pass

class n1(single):
pass
class n2(single):
pass
class n3(single):
pass

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 19 '05 #4
Riccardo Galli wrote:
Hi all.
It's easier if I show an example first.

Say I have

class A(object):
def speak(self): print 'A!'

class B(object):
def speak(self): print 'B!'

I also have a factory function like this.

def foo(kind,*args,**kwds):
if kind=='a': return A(*args,**kwds)
else: return B(*args,**kwds)

I need foo to be a class, so that I could inherit from it and still use
it as a factory, so that I can do:

Foo('a').speak()
Foo('b'.speak()

class Final(Foo):
def __init__(self,kind,*args,**kwds):
super(Foo,self).__init__(kind,*args,*kwds)


I'm not clear why you want to do this. Presumably you want to be able to say
Final('a').speak() ??
How will this differ from
Foo('a').speak()
Why does Final need to subclass Foo?

Can you use either of these syntaxes? Either one can be specialized in a subclass:
Foo()('a').speak() # use Foo.__call__() as factory
Foo.make('a') # Use classmethod as factory

Kent
Jul 19 '05 #5
[Michele Simionato]
Having a class that returns instances of some other class is horrible,
[...] don't do it!


Unusual, granted. Horrible, why?

I found useful, sometimes, (ab)using Python syntax for representing data
having more or less complex structure. More than once, it spared me the
trouble of inventing a syntax, and then writing a lexer and a parser.
Letting class constructors returning arbitrary objects has often been
useful in such circumstances.

--
François Pinard http://pinard.progiciels-bpi.ca
Jul 19 '05 #6
Returning instances of some other class is not so horrible. They're
called FactoryMethods usually.

An example is when you have a polymorphic tree of image file reader
objects, and you open an image file, it might return a JpegReader
which ISA ImageReader or a TIFFReader which also ISA ImageReader, but
you want all of the 'which class do I need for this data?' logic to
be in one place.

-Jim

On 15 Jun 2005 03:12:07 -0700, Michele Simionato
<mi***************@gmail.com> wrote:
Having a class that returns instances
of some other class is horrible, but
since you asked for it:

class A(object): pass
class B(object): pass

class Foo(object):
def __new__(cls, arg):
if arg=="a":
return A()
else:
return B()

print Foo("a")
print Foo("b")

Michele Simionato

P.S. don't do it!

--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #7
Riccardo Galli wrote (approximately):
I need a class to behave as a factory method for its children so that I could inherit
from it and still use it as a factory.


Several others suggest you probably don't want to do this; I concur.
Separating the factory from the interface (I presume you are doing some
isinstance checking) is probably a good idea. If, for some reason, you
feel you must implement your original plan, the following code should
outline the trick (look up __new__ and study the code).
class Interf(object):
def __new__(klass, kind, *args, **kwargs):
if kind is None:
return object.__new__(klass, *args, **kwargs)
elif kind == 'a':
return Sub1(*args, **kwargs)
else:
assert kind == 'b'
return Sub2(*args, **kwargs)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, ', '.join(
'%s=%r' % pair
for pair in sorted(vars(self).items())))

class Sub1(Interf):
def __new__(klass, *args, **kwargs):
return Interf.__new__(klass, None, *args, **kwargs)
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

class Sub2(Interf):
def __new__(klass, *args, **kwargs):
return Interf.__new__(klass, None, *args, **kwargs)
def __init__(self, *args, **kwargs):
self.xargs = args
self.xkwargs = kwargs

--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #8
Yes, factory methods, exactly. If I had a base class ImageReader with
children JpegReader and TIFFReader, I will probably add two factory
methods ImageReader.makeJpegReader and ImageReader.makeTIFFReader, I
will not override ImageReader.__new__
to return sometimes JpegReader instances and sometimes TIFFReader
instances. Explicit is better than implicit and all that.

Michele Simionato

Jul 19 '05 #9
I think using __new__ when a plain old factory method would
work is horrible. But I am not saying that there no use cases for
__new__. I used it once, when I wanted a class working
for the final user as a factory function. I just wrote the class in
lower case and gave it a custom __new__ method.

Michele Simionato

Jul 19 '05 #10
Oh, I see. Yeah, having the code look like you're instantiating one
class, but really getting a different one is really horrible. Sorry I
didn't catch on to the subtlety. I'm always complaining about code
that looks like it does one thing, but really does another.

-Jim

On 15 Jun 2005 22:24:28 -0700, Michele Simionato
<mi***************@gmail.com> wrote:
I think using __new__ when a plain old factory method would
work is horrible. But I am not saying that there no use cases for
__new__. I used it once, when I wanted a class working
for the final user as a factory function. I just wrote the class in
lower case and gave it a custom __new__ method.

Michele Simionato

--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #11
On Wed, 15 Jun 2005 11:50:27 +0200, Riccardo Galli wrote:

Hi.
Thanks to all who answered.

Scott David posted a solution (clever, scott) which was what I asked for,
but I noticed some oddness while using it (about __init__).
I also understood that this way should be avoided.

I ended up using a proxy approach, wich works.

Thank you,
Riccardo

--
Riccardo Galli
Sideralis Programs
http://www.sideralis.net
Jul 19 '05 #12

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

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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 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.