Is this a legal / acceptable statement ?

Martin Blume mblume at socha.net
Fri May 5 17:16:36 CEST 2006


"Philippe Martin" schrieb 
> Hi,
> 
> This code works, but is it "appropriate" ?
> 
> l_init = False
> 
> if True == l_init and 1234 = l_value:
>  print 'l_value is initialized'
> 
> I know I can do this with a try but ...
> 
I am a Python newbie, but I think working with
l_value = None 
would be the most pythonic way.

C:\>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19) 
   [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" 
    for more information.
>>> v = None
>>> x = 2
>>> x
2
>>> v
>>> v_default=3
>>> y = x + (v or v_default)
>>> y
5
>>> v = 6
>>> y = x + (v or v_default)
>>> y
8
>>> v = None
>>> y = x + (v or v_default)
>>> y
5
>>>

Of course in a function you can use:
def travel_time(from, to, speed=60):
    pass

and if travel_time is called 
travel_time(a,b,1000) the speed will be 1000
and if travel_time is called
travel_time(a,b) the speed will be 60

IMHO.
Martin








More information about the Python-list mailing list