
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] variable assignments inside expressions in python
On Tue, 2007-07-10 at 16:01 +0900, Hector Akamine wrote:
> Hi,
>
> In C, I can do a variable assignment inside a while expression, like:
>
> while ( (myvar = get_time( ) ) < DEADLINE) {
> /* use the value of myvar somewhere*/
> ...
> }
>
> Trying to do the same in python
> while ( (myvar = get_time( ) ) < DEADLINE):
>
> throws an error. Is there a "python way" to do this? (I have googled
> a little and the answer seems that I can't, but I refuse to accept it
> :-) )
In the "Ppython Tutorial Release 2.5" at the end of 5.7 you will find a
note about this behaviour.
http://docs.python.org/tut/node7.html#SECTION007700000000000000000
That said the following is some sample code that will allow you to do
something like what you are asking.
************start code********************
#!/usr/bin/python
def get_time(n):
return n;
deadline = [1,2,3,4,5,6];
for i in deadline:
myvar = get_time(i)
print myvar
***********end code***********************
>
> Thanks,
> Hector
>
Home |
Main Index |
Thread Index