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

effbot at pythonware.com effbot at pythonware.com
Fri Sep 8 13:24:06 CEST 2000

olav wrote:
> A beginners question:
> I have a function:
> def get_attr( _attrs, _key ):
>         if _attrs.has_key( _key ):
>                 return _attrs[ _key ]
>         else:
>                 return None

Note that you're emulating a standard dictionary
method here.  Use _attrs.get(key) instead.

> That I would like to use about like this
> if ( attr = get_attr( attrs, 'title'))
> (this is C-syntax)
> Though its not accepted by the interpreter.
> Neither is the more Pythonlike:
> if attr = get_attr( attrs, 'title')

In Python, assignment is a statement, not an operator.

> How can I do this in Python?

Press the return key:

   attr = attrs.get("title")
   if attr:


<!-- daily news from the python universe:

Sent via Deja.com http://www.deja.com/
Before you buy.

More information about the Python-list mailing list