Skip to main content
3
Karl Knechtel
  • 61.3k
  • 14
  • 131
  • 193
Post Closed as "Duplicate" by cs95 python
Fixed typo in title; put ariable names and Python keyords into backticks.
Source Link
PM 2Ring
  • 55.6k
  • 6
  • 96
  • 201

Assignning Assigning string with boolean expression

I am trying to understand this code from someone else's project. If you want the context itsit's here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2IS_PY2 is just a boolean variable, trueTrue if the pythonPython major version is 2. I know that a non-empty string is TrueTrue, but for some reason I don't understand openmodeopenmode is assigned either w'w' or wt'wt' rather than TrueTrue or FalseFalse.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}

Could someone explain the result?

Assignning string with boolean expression

I am trying to understand this code from someone else's project. If you want the context its here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2 is just a boolean variable, true if the python major version is 2. I know that a non-empty string is True, but for some reason I don't understand openmode is assigned either w or wt rather than True or False.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}

Could someone explain the result?

Assigning string with boolean expression

I am trying to understand this code from someone else's project. If you want the context it's here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2 is just a boolean variable, True if the Python major version is 2. I know that a non-empty string is True, but for some reason I don't understand openmode is assigned either 'w' or 'wt' rather than True or False.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}

Could someone explain the result?

Source Link
Mascros
  • 361
  • 1
  • 4
  • 12

Assignning string with boolean expression

I am trying to understand this code from someone else's project. If you want the context its here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2 is just a boolean variable, true if the python major version is 2. I know that a non-empty string is True, but for some reason I don't understand openmode is assigned either w or wt rather than True or False.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}

Could someone explain the result?