Vertical bar in Python bitwise assignment operator

前端 未结 4 785
情话喂你
情话喂你 2020-12-17 09:07

There is a code and in class\' method there is a line:

object.attribute |= variable

I can\'t understand what it means. I didn\'t find (|=)

4条回答
  •  [愿得一人]
    2020-12-17 09:22

    I should add that "bar-equals" is now (in 2018) most popularly used as a set-union operator to append elements to a set if they're not there yet.

    >>> a = {'a', 'b'}
    >>> a
    set(['a', 'b'])
    
    >>> b = {'b', 'c'}
    >>> b
    set(['c', 'b'])
    
    >>> a |= b
    >>> a
    set(['a', 'c', 'b'])
    

    One use-case for this, say, in natural language processing, is to extract the combined alphabet of several languages:

    alphabet |= {unigram for unigram in texts['en']}
    alphabet |= {unigram for unigram in texts['de']}
    ...
    

提交回复
热议问题