C
cfriedalek
Sorry for the vague subject. Not sure what the right terminology is.
How can I use an instance's data by reference to the instance name,
not the instance attribute? OK the question is probably really poor
but hopefully an example will make it clear.
In this case x is an integer. My understanding is that x in an
instance of an integer class. Since it refers to only a single value
things like print x, 3*x etc operate on the instance name which seems
to refer to the instance data, not the instance itself. I want to do
the same for my own classes.
For example:
self.val = val
'int' and 'instance
I have been able to do this by overriding __getitem__ when self.val is
a sequence. But I can't find out what to do when self.val is a simple
type like int, float etc.
How can I use an instance's data by reference to the instance name,
not the instance attribute? OK the question is probably really poor
but hopefully an example will make it clear.
3x=1
type(x)
2
print x 1
3*x
In this case x is an integer. My understanding is that x in an
instance of an integer class. Since it refers to only a single value
things like print x, 3*x etc operate on the instance name which seems
to refer to the instance data, not the instance itself. I want to do
the same for my own classes.
For example:
def __init__(self,val):class y:
self.val = val
y1 = y(10)
print y1
<type 'exceptions.TypeError'>: unsupported operand type(s) for *:3*y1
'int' and 'instance
I have been able to do this by overriding __getitem__ when self.val is
a sequence. But I can't find out what to do when self.val is a simple
type like int, float etc.