a=[ lambda t: t**n for n in range(4) ]

tiissa tiissa at nonfree.fr
Sat Apr 23 06:20:39 EDT 2005


mehmetmutigozel at gmail.com wrote:
> i wonder if there is an automatic way to make that without calling a
> function.

You mean without _explicitely_ calling a function.
May I inquire why you need to write f instead of f(x)?

> an automatic way that depends on changing the value of x. as each time
> x=something used the whole matrix changes automaticly.

As pointed out by El Pitonero property can be your friend:

  >>> class A(object):
  ...     def get_v(self):
  ...             return [1,x]
  ...     v=property(get_v)
  ...
  >>> a=A()
  >>> x=0
  >>> a.v
  [1, 0]
  >>> x=2
  >>> a.v
  [1, 2]
  >>>

However, you will calculate your matrix each time you will access it 
(and I find it ugly to use a global variable this way).

You can however build it the other way round:

  >>> class B(object):
  ...     def __init__(self):
  ...             self.v=[1,0]
  ...             self.x=0
  ...     def calc_v(self):
  ...             self.v[1]=self.__x
  ...     def set_x(self,x):
  ...             self.__x=x
  ...             self.calc_v()
  ...     def get_x(self):
  ...             return self.__x
  ...     x=property(get_x, set_x)
  ...
  >>> b=B()
  >>> b.v
  [1, 0]
  >>> b.x=2
  >>> b.v
  [1, 2]
  >>>

This time, calculations only occur when you change the value of x.



More information about the Python-list mailing list