M
manstey
Hi,
My question probably reflects my misunderstanding of python objects,
but I would still like to know the answer.
The question is, is it possible for an instnace to have a value (say a
string, or integer) that can interact with other datatypes and be
passed as an argument?
The following code of course gives an error:
class Test(object):
def __init__(self, val):
self.val = val
TypeError: unsupported operand type(s) for +: 'Test' and 'str'
Is there a way to make a have the value a.val when it is used as
above, or as an argument (eg function(a, 10, 'sdf') etc)?
The only fudge I discovered for simple addition was to add to the
class
def __add__(self, obj):
return a.val + obj
but this doesn't solve the problem in general. I have tried
subclassing the string type, but as it is immutable, this is not
flexible the way a.val is (i.e. it can't e reassigned and remain a
subclass).
Any pointers, or is my question wrong-headed?
btw, my motivation is wanting to mimic another oo language which
allows this, so it allows:
Canberra %String
etc.
We have had to implement Person.Address as Person.Address.val, making
Address an instance with .val, .type, etc.
My question probably reflects my misunderstanding of python objects,
but I would still like to know the answer.
The question is, is it possible for an instnace to have a value (say a
string, or integer) that can interact with other datatypes and be
passed as an argument?
The following code of course gives an error:
class Test(object):
def __init__(self, val):
self.val = val
TypeError: unsupported operand type(s) for +: 'Test' and 'str'
Is there a way to make a have the value a.val when it is used as
above, or as an argument (eg function(a, 10, 'sdf') etc)?
The only fudge I discovered for simple addition was to add to the
class
def __add__(self, obj):
return a.val + obj
but this doesn't solve the problem in general. I have tried
subclassing the string type, but as it is immutable, this is not
flexible the way a.val is (i.e. it can't e reassigned and remain a
subclass).
Any pointers, or is my question wrong-headed?
btw, my motivation is wanting to mimic another oo language which
allows this, so it allows:
Canberra %String
etc.
We have had to implement Person.Address as Person.Address.val, making
Address an instance with .val, .type, etc.