Return and set

Paul Woolcock pwoolcoc at gmail.com
Tue Jul 19 09:33:15 EDT 2011


I don't know if this is better (or more "pythonic"), but it works for me on
python 2.7.

>>> class MyKlass(object):
...     def __init__(self, tok):
...         self.tok = tok
...     def gettoken(self):
...         t, self.tok = self.tok or None, None
...         return t

On Tue, Jul 19, 2011 at 9:23 AM, Billy Mays <
81282ed9a88799d21e77957df2d84bd6514d9af6 at myhashismyemail.com> wrote:

> I have a method getToken() which checks to see if a value is set, and if
> so, return it.  However, it doesn't feel pythonic to me:
>
> def getToken(self):
>    if self.tok:
>        t = self.tok
>        self.tok = None
>        return t
>    # ...
>
>
> Is there a way to trim the 'if' block to reset self.tok upon return?
>
> --
> Bill
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 

Paul Woolcock
pwoolcoc at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110719/908182e0/attachment-0001.html>


More information about the Python-list mailing list