Re: using identifiers before they are defined
You should define the function first and then call it. def something(i): return i a = something(5) If you want a reference to the function somewhere else you can do this: global alias = something print alias(i) On Tue, Jun 12, 2012 at 1:53 PM, Julio Sergio wrote: > I'm puzzled with the following example, which is intended to be a part of a > module, say "tst.py": > > a = something(5) > > def something(i): > return i > > > > When I try: > > ->>> import tst > > The interpreter cries out: > > Traceback (most recent call last): > File "", line 1, in > File "tst.py", line 11, in >a = something(5) > NameError: name 'something' is not defined > > I know that changing the order of the definitions will work, however there > are > situations in which referring to an identifier before it is defined is > necessary, e.g., in crossed recursion. > > So I modified my module: > > global something > > a = something(5) > > > def something(i): > return i > > > And this was the answer I got from the interpreter: > > ->>> import tst > > Traceback (most recent call last): > File "", line 1, in > File "tst.py", line 12, in >a = something(5) > NameError: global name 'something' is not defined > > > Do you have any comments? > > Thanks, > > --Sergio. > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Seems like what you need is from othermodule import bb def aa(): bb() On Tue, Jun 12, 2012 at 2:51 PM, Ethan Furman wrote: > Julio Sergio wrote: > >> Jose H. Martinez gmail.com> writes: >> >> >>> You should define the function first and then call it. >>> >>> >>> def something(i): return i >>> >>> >>> a = something(5) >>> >>> >>> If you want a reference to the function somewhere else you can do this: >>> >>> >> I know that. That was what I meant by "changing the order of the >> definitions will work" in my original message. >> >> And I insist in the issue, which is not trivial... In my message I >> mentioned "crossed recursion", and I delve into it here: >> >> Suppose I have to define two functions, aa, and, bb that are designed to >> call each other: >> >> def aa(): >> ... >> ... a call of bb() somewhere in the body of aa >> ... >> >> def bb(): >> ... >> ... a call of aa() somewhere in the body of bb >> ... >> >> >> Whatever the order of definition of aa and bb the problem remains >> > > No. The reply from MRAB explains this. > > ~Ethan~ > -- > http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list> > -- http://mail.python.org/mailman/listinfo/python-list
Re: string to list
string.split(',') will give you an array. Example: 'AAA,",,",EEE,FFF,GGG '.split(',') ['AAA', '"', '', '"', 'EEE', 'FFF', 'GGG'] On Wed, Jun 13, 2012 at 10:53 PM, Chris Rebert wrote: > n Wed, Jun 13, 2012 at 7:29 PM, bruce g wrote: > > What is the best way to parse a CSV string to a list? > > Use the `csv` module: > http://docs.python.org/library/csv.html > http://www.doughellmann.com/PyMOTW/csv/ > > The `StringIO` module can be used to wrap your string as a file-like > object for consumption by the `csv` module: > http://docs.python.org/library/stringio.html > > Cheers, > Chris > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: lazy evaluation of a variable
Another option would be to refactor your function so that it is a generator expression using the yield keyword. On Sun, Jun 17, 2012 at 7:40 PM, Peter Otten <__pete...@web.de> wrote: > Gelonida N wrote: > > > I'm having a module, which should lazily evaluate one of it's variables. > > Meaning that it is evaluated only if anybody tries to use this variable. > > > > At the moment I don't know how to do this and do therefore following: > > > > > > ### mymodule.py ### > > var = None > > > > def get_var(): > > global var > > if var is not None: > > return var > > var = something_time_consuming() > > > > > > > > Now the importing code would look like > > > > import mymodule > > def f(): > > var = mymodule.get_var() > > > > The disadvantage is, that I had to change existing code directly > > accessing the variable. > > > > > > I wondered if there were any way to change mymodule.py such, that the > > importing code could just access a variable and the lazy evaluation > > would happen transparently. > > > > import mymodule > > def f(): > > var = mymodule.var > > > > > > > > Thanks in advance for you suggestions > > You can inject arbitrary objects into sys.modules: > > >>> import sys > >>> class MyModule(object): > ... def __init__(self): > ... self._var = None > ... @property > ... def var(self): > ... result = self._var > ... if result is None: > ... print "calculating..." > ... self._var = result = 42 > ... return result > ... > >>> sys.modules["mymodule"] = MyModule() > >>> import mymodule > >>> mymodule.var > calculating... > 42 > >>> mymodule.var > 42 > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list