if( result = function()) in Python - or reference arguments?

Quinn Dunkan quinn at ngwee.ugcs.caltech.edu
Fri Sep 8 07:21:04 EDT 2000


On Fri, 08 Sep 2000 12:38:48 +0200, Olav <OlavB at yahoo.com> wrote:
>I have a function:
>def get_attr( _attrs, _key ):
>        if _attrs.has_key( _key ):
>                return _attrs[ _key ]
>        else:
>                return None
>
>That I would like to use about like this
>if ( attr = get_attr( attrs, 'title'))
>(this is C-syntax)

attr = get_attr(attrs, 'title')
if attr:
    ...

'=' is a statement in python, not an expression.  This gets brought up quite
frequently (I did myself once *blush*).

But if attrs is just a dict, why not:

if attrs.has_key('title'):
    [ stuff using attrs['title'] ]

You also might be interested in the dict 'get' method.

>It could be OK if I could do something like
>if get_attr( attr, attrs, 'title')

Eeek... no, let's leave 'return parameters' to C :)



More information about the Python-list mailing list