Re: find isset() php function equivalent in python
Olivier Noblanc ATOUSOFT wrote:[color=blue]
> Hello
>
>
> What is the equivalent function of php isset() in python
>
> Thank you very much.
>
> olivier noblanc
> http://www.logiciel-erp.fr
>
>[/color]
try:
if variable:
# isset
pass
except NameError:
# not set
pass
could work...
--
--------------------------------------
Ola Natvig <ola.natvig@inf osense.no>
infoSense AS / development
Re: find isset() php function equivalent in python
Ola Natvig wrote:[color=blue]
> Olivier Noblanc ATOUSOFT wrote:
>[color=green]
>> Hello
>>
>>
>> What is the equivalent function of php isset() in python[/color]
>
> try:
> if variable:
> # isset
> pass
> except NameError:
> # not set
> pass
>[/color]
you could use:
[color=blue][color=green][color=darkred]
>>> 'variable' in vars()[/color][/color][/color]
But be aware that it is bad bad practice to do it like that.
If you need variables that you don't know that name of, you should put
them in a dictionary. They are made for that exact purpose.
[color=blue][color=green][color=darkred]
>>> unkown_vars = {}
>>> unkown_vars['variable'] = 42
>>> 'variable' in unkown_vars[/color][/color][/color]
True
Re: find isset() php function equivalent in python
Max M wrote:[color=blue]
> Ola Natvig wrote:
>[color=green]
>> Olivier Noblanc ATOUSOFT wrote:
>>[color=darkred]
>>> Hello
>>>
>>>
>>> What is the equivalent function of php isset() in python[/color]
>>
>>
>> try:
>> if variable:
>> # isset
>> pass
>> except NameError:
>> # not set
>> pass
>>[/color]
>
>
> you could use:
>[color=green][color=darkred]
> >>> 'variable' in vars()[/color][/color]
>
> But be aware that it is bad bad practice to do it like that.
>
> If you need variables that you don't know that name of, you should put
> them in a dictionary. They are made for that exact purpose.
>[color=green][color=darkred]
> >>> unkown_vars = {}
> >>> unkown_vars['variable'] = 42
> >>> 'variable' in unkown_vars[/color][/color]
> True
>
>[/color]
If it's a greater possibility that the 'variable' are set than it's not
you will get better performance when using:
try:
print unknown_vars['variable']
except KeyError:
print 'variable are not set'
istead of
if 'variable' in unknown_vars:
print unknown_vars['variable']
else:
print 'variable are not set'
You could even use
print unknown_vars.ge t('variable', 'variable are not set')
dictionary.get( key, default) returns the default if key are not located
in the dictionary, I'm not sure if the function uses the try / except
KeyError aproach or what it uses.
--
--------------------------------------
Ola Natvig <ola.natvig@inf osense.no>
infoSense AS / development
Comment