CHW
March 10th, 2010, 06:16 PM
Hello,
I want to use the following code:
class Test(tuple):
def __new__(self, n=None, k=None, d=None):
# t = do some processing...
return tuple.__new__(self,t)
def __call__(self, c):
return c*2
This works as expected, I can create a tuple and call it.
For better readability I would like to use a namedtuple, but here I fail.
I found one method on the python.org site, which is callable, but I don't manage to define __new__ to be able to make some input processing.
class Point(collections.namedtuple('Point', 'x y')):
def __call__(self,x):
return x*2
I tried around and managed to "inherit" a namedtuple using the __new__ method:
class Test(tuple):
def __new__(self, n=None, k=None, d=None):
cpara = collections.namedtuple('Foo', 'n, k, d')
# t = some processing...
return cpara._make(t)
def __call__(self, c):
c*2
Well, here I get the namedtuple I would like to have, but the object is not callable...
Does anyone knows how to solve this issue?
Regards,
CHW
I want to use the following code:
class Test(tuple):
def __new__(self, n=None, k=None, d=None):
# t = do some processing...
return tuple.__new__(self,t)
def __call__(self, c):
return c*2
This works as expected, I can create a tuple and call it.
For better readability I would like to use a namedtuple, but here I fail.
I found one method on the python.org site, which is callable, but I don't manage to define __new__ to be able to make some input processing.
class Point(collections.namedtuple('Point', 'x y')):
def __call__(self,x):
return x*2
I tried around and managed to "inherit" a namedtuple using the __new__ method:
class Test(tuple):
def __new__(self, n=None, k=None, d=None):
cpara = collections.namedtuple('Foo', 'n, k, d')
# t = some processing...
return cpara._make(t)
def __call__(self, c):
c*2
Well, here I get the namedtuple I would like to have, but the object is not callable...
Does anyone knows how to solve this issue?
Regards,
CHW