Python - is there a “don't care” symbol for tuple assignments?

前端 未结 9 1823
你的背包
你的背包 2020-12-03 04:17

Given a string \"VAR=value\" I want to split it (only) at the first \'=\' sign (< value > may contain more \'=\' signs), something like this:

<         


        
相关标签:
9条回答
  • 2020-12-03 04:52

    As others have said, underscore (_) is the standard. But if underscore is being used for translations, I think double underscore is the best alternative.

    var, __, value = "VAR=value".partition('=')

    Better than these:

    var, unused, value = "VAR=value".partition('=')

    var, unused_del, value = "VAR=value".partition('=')

    var, _del, value = "VAR=value".partition('=')

    0 讨论(0)
  • 2020-12-03 04:54

    Really strange question, because you can do just:

    var, _, value = s.partition(sep)
    

    and don't care about _ variable, but _ is just a name as sep, as var or value. By the way use str.split

    >>> var, value = "VAR=value".split('=')
    >>> var, value
    ('VAR', 'value')
    >>> 
    
    0 讨论(0)
  • 2020-12-03 04:55
    • Python doesn't have syntax to avoid assignment in unpacking and such.

    • As others have mentioned, there is a convention of using _ for variables you don't care about. This is fairly broadly used and understood, but personally I think it is underused. If you say var, _, value = "VAR=value".partition('='), you have to know what's going on to know what the _ was and why you didn't care about it when you read the code. If you say var, sep, value you document at the same time. This isn't very important for str.partition, but I've seen _, _, name, _, city, _ = some_weird_function() before and found it less useful than if everything was unpacked to useful names.

    • You could technically use str.split here if you wanted to. var, value = "foo=bar=baz".split("=", 1).

    0 讨论(0)
  • 2020-12-03 05:00

    Why don't you use 'VAR=value'.split('=') instead? That disregards the delimiter.

    EDIT (to accommodate Cristi's example in the comment):

    From Diving into Python:

    Tip: anystring.split(delimiter, 1) is a useful technique when you want to search a string for a substring and then work with everything before the substring (which ends up in the first element of the returned list) and everything after it (which ends up in the second element).

    0 讨论(0)
  • 2020-12-03 05:01

    There isn't anything official in the language for that; you can just use any throw-away variable. As far as standards go, I've seen underscores used occasionally in Python and other languages. The only issue there is that underscore is used as an alias for gettext when localizing. But if you aren't doing localization, or aren't using the global-binding for it, then underscore should work fine.

    0 讨论(0)
  • 2020-12-03 05:04

    The _ is commonly used as a name for you dont care * For example, in a tuple containing the name, surname and nickname in a moment in which we are interested only in name and surname, could use _ to indicate that the name is not important at this point:

    data = ('John', 'Mate', 'Little John')
    name, surname, _ = data
    
    0 讨论(0)
提交回复
热议问题