string literal or NoneType
castironpi at gmail.com
castironpi at gmail.com
Sat Mar 15 18:28:59 EDT 2008
On Mar 15, 10:27 am, menosa... at gmail.com wrote:
> hi all
> i want to check a condition and if true should return a filename
> string from a list.if the condition is false i am returning a
> "" (string literal)..
>
> retv=""
> if somecondition:
> retv=mylist[x]
> ...
> return retv
>
> The calling function will check the return value and print the
> filename if it is not """.
> in the calling function i can code
> if returnval !="":
> print "filename is:",returnval
> else:
> print "no filename found"
>
> what i want to know is ,should i rewrite the if returnval !="" as
>
> if returnval:
> print "filename is:",returnval
> else:
> print "no filename found"
>
> or is the way i coded the right way ? i am little confused here
> Someone suggested that i make a variable retv None and if the
> condition true then set retv as filename ,
>
> retv=None
> if somecondition:
> retv=mylist[x]
>
> ...
> return retv
>
> which approach is correct..? can someone help please
> vincent
One possibility:
def fun( ..., testpair, ... ):
if somecondition:
testpair[ True ]( mylist[x] )
else:
testpair[ False ]()
. However 'fun' knows "too much for its scope" by varying call
signature.
Call it by:
fun( ...,
{ True: partial( print, 'filename is:' ),
False: partial( print, 'no filename found' )
}, ... )
, which relies even MORE on the particulars of how 'fun' calls.
Reversed:
return somecondition, partial( mylist.__getitem__, x )
cond, dataf= fun( ... )
if cond:
print( 'filename is ', dataf() )
else:
print( 'no filename found' )
, which leave different amounts of genericity/genericness/generality
to work in.
More information about the Python-list
mailing list