473,322 Members | 1,781 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,322 software developers and data experts.

Good idea to use a class as function with __new__?

Hi,

I am implementing som code generation and want to to use some variant
of the template method pattern.

What I came up with is to have a class with the common part
in a method and the subclasses can then override the Customize methods
to do their own special part.

Now to the question I use the __new__ to return the result instead
of the instance. So that the class is used as an function.

So insted of having.

a = Template()
result = a.__TemplateMethod(preifix="foo")

I can write:

result = Template(prefix="foo")

class Template(object):

def __init__(cls, *args, **kwds):
pass

def __new__(cls, *args, **kwds):
obj = super(Template, cls).__new__(cls, *args, **kwds)
return obj.__TemplateMethod(*args, **kwds)

def Customize1(self, prefix="hello", *args, **kwds):
return prefix+"1\n"

def Customize2(self, prefix="hello", *args, **kwds):
return prefix+"2\n"

def __TemplateMethod(self, *args, **kwds):
result = ""
result += self.Customize1(*args, **kwds)
result += self.Customize1(*args, **kwds)
return result
b = Template("foo")

print b

May 28 '07 #1
2 977
En Mon, 28 May 2007 09:17:30 -0300, glomde <tb****@yahoo.comescribió:
I am implementing som code generation and want to to use some variant
of the template method pattern.

What I came up with is to have a class with the common part
in a method and the subclasses can then override the Customize methods
to do their own special part.

Now to the question I use the __new__ to return the result instead
of the instance. So that the class is used as an function.
It works, and I don't see any big problems, but I don't *like* that.
I'd use __call__ instead; that is, write __new__ and __init__ normally -if
you need them at all- and move the magic to __call__:

def __call__(self, *args, **kwds):
return self.__TemplateMethod(*args, **kwds)

x = Template()(prefix="foo")
or perhaps:
x = Template(prefix="foo")()

I think the extra () improves readability - it's clear that x comes from a
function call, it's not a Template instance as one would expect from your
original code.
And depending on the application, you could keep the instances and call
them with different arguments - with the original code you always create a
new instance just to discard it immediately.

--
Gabriel Genellina

May 28 '07 #2

Gabriel Genellina wrote:
def __call__(self, *args, **kwds):
return self.__TemplateMethod(*args, **kwds)

x = Template()(prefix="foo")
or perhaps:
x = Template(prefix="foo")()

I think the extra () improves readability - it's clear that x comes from a
function call, it's not a Template instance as one would expect from your
original code.
And depending on the application, you could keep the instances and call
them with different arguments - with the original code you always create a
new instance just to discard it immediately.

--
Thanks for the reply. I was considering __call__ but in my case it is
really something
that will be discarded immediately. And right now it is is implemented
as function
where I have arguments to send in which customize functions something
like:

def Template(self, func1=Customize1, func2= Customize2, *args,
**kwds):
func1(*args, **kwds)
func2(*args, **kwds)

So I just wanted to organize it a little bit nicer and be able to
subclass instead of
sending in function pointers. But that had the same interface.

But you are right the extra () does make it clearer, so I probably go
for that.

Cheers,

/T
Gabriel Genellina
May 28 '07 #3

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

Similar topics

5
by: Todd Johnson | last post by:
Ok, say I have a class MyClass and an __init__(self, a, b) Say that a and b are required to be integers for example. So my init looks like: __init__(self, a, b): try: self.one = int(a)...
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
9
by: Joel Hedlund | last post by:
Hi! I need some input on my use of metaclasses since I'm not sure I'm using them in a pythonic and graceful manner. I'm very grateful for any tips, pointers and RTFMs I can get from you guys. ...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
9
by: Gregor Horvath | last post by:
Hi, I want to reference a class itself in its body: class SomeElement(object): def __init__(self, mycontainer): self.mycontainer=mycontainer class SomeContainer(object): a =...
2
by: Steven D'Aprano | last post by:
Here's a simple class-factory function that returns a sub-class of the old-style class it is passed. def verbosify_oclass(klass): """Returns a verbose sub-class of old-style klass.""" class...
7
by: Jeffrey Barish | last post by:
(Pdb) myclass MyClass( 0, 0, 'A string', 123.45) (Pdb) copy.copy(myclass) *** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',) I see 4 arguments (actually, 5 because...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
0
isladogs
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...

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.