Results 1 to 10 of 10

Thread: Python shortcut for variable assignment if not None

  1. #1
    Join Date
    Mar 2006
    Location
    Portland, OR USA
    Beans
    169
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Python shortcut for variable assignment if not None

    Is there a pythonic shortcut to:

    Code:
    if some_value is not None:
        my_dict['key'] = some_value
    - Micah Carrick | www.micahcarrick.com | GTK+ Forums

  2. #2
    Join Date
    Mar 2006
    Location
    Portland, OR USA
    Beans
    169
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Python shortcut for variable assignment if not None

    I should add that my_dict['key'] is not defined before. In other words,

    my_dict['key'] = some_value or None

    will not work because the key 'key' shouldn't even exist in the dictionary if some_value is none.
    - Micah Carrick | www.micahcarrick.com | GTK+ Forums

  3. #3
    Join Date
    May 2006
    Beans
    1,790

    Re: Python shortcut for variable assignment if not None

    Quote Originally Posted by MicahCarrick View Post
    I should add that my_dict['key'] is not defined before. In other words,

    my_dict['key'] = some_value or None

    will not work because the key 'key' shouldn't even exist in the dictionary if some_value is none.
    One question is whether you want an existing value to disappear in case some_value is None. Of course that's not what your code does, but sometimes it may be the proper thing to do.

    (Not being a python expert, I dare not suggest that anything in particular is pythonic.)

  4. #4
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python shortcut for variable assignment if not None

    I can't really think of a shortcut to do that. Maybe you can remove the `is not None' part depending on the type some_value.
    But what is the problem with using the if statement?

  5. #5
    Join Date
    Mar 2006
    Location
    Portland, OR USA
    Beans
    169
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Python shortcut for variable assignment if not None

    But what is the problem with using the if statement?
    Nothing. My background is in C and PHP so I often overlook some of the beauty of python (like list comprehension). I have to make a conscious effort to take a second look at my code looking for pythonic ways to refactor bits.

    I thought this may be one of those situations. Perhaps not.

    Thanks everybody.
    - Micah Carrick | www.micahcarrick.com | GTK+ Forums

  6. #6
    Join Date
    Aug 2006
    Beans
    458
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python shortcut for variable assignment if not None

    Quote Originally Posted by MicahCarrick View Post
    Is there a pythonic shortcut to:

    Code:
    if some_value is not None:
        my_dict['key'] = some_value

    Quote Originally Posted by MicahCarrick View Post
    I should add that my_dict['key'] is not defined before. In other words,

    my_dict['key'] = some_value or None

    will not work because the key 'key' shouldn't even exist in the dictionary if some_value is none.
    Something like http://docs.python.org/library/stdty...ict.setdefault?
    Last edited by navneeth; November 15th, 2010 at 08:27 PM.

  7. #7
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,524
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Python shortcut for variable assignment if not None

    surely [if some_value is not None:] is the same as [if some_value:]
    Catch me on Freenode - imark

  8. #8
    Join Date
    May 2006
    Beans
    1,790

    Re: Python shortcut for variable assignment if not None

    Quote Originally Posted by CaptainMark View Post
    surely [if some_value is not None:] is the same as [if some_value:]
    some_value = 0

  9. #9
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Python shortcut for variable assignment if not None

    I don't see the reason why you'd want a shortcut for that...

  10. #10
    Join Date
    Aug 2007
    Location
    UK
    Beans
    427
    Distro
    Ubuntu UNR

    Re: Python shortcut for variable assignment if not None

    If you want to assign a string in place of an empty string you can do code like:
    Code:
    dict[key] = input or default_string
    This is a forced assignment unlike the code in your example which is why you need the if statement.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •