define
Anton Muhin
antonmuhin at sendmail.ru
Thu May 8 10:51:19 EDT 2003
Turhan Ozen wrote:
> in c it is possible to define
>
> #define PH x[1]
>
> so PH = 3 means x[1]=3
>
> how can I do this in python?
>
Although quite agree with all the previous posters that you shouldn't do
it (even in C++ there is a better way: something like float &ref =
x[1]). However, if you are deadly sure what are you doing you can use
something like above:
>>> class ListRef:
... def __init__(self, l, ind):
... self.__list = l
... self.__ind = ind
... def set(self, val):
... self.__list[self.__ind] = val
... def get(self):
... return self.__list[self.__ind]
...
>>> lr = ListRef(l, 0)
>>> lr.get()
3
>>> l
[3, 5, 5, 5, 5]
>>> lr.set(5)
>>> l
[5, 5, 5, 5, 5]
>>> lr.set(7)
>>> l
[7, 5, 5, 5, 5]
>>>
hth,
anton.
More information about the Python-list
mailing list