Can I conditionally choose what variable I assign a value to?

不羁岁月 提交于 2019-12-14 01:36:26

问题


I know that you can do things like this in Python:

var = value1 if( booleanCheck() ) else value2

What I want to know is if I can conditionally choose which var I place a value into with a similar sort of structure?

something like this:

(var1 if( booleanCheck() ) else var2) = value

In my case specifically I'm trying to assign a child node to the correct side in a Binary Search Tree, I know I can just do a normal if block but I was trying to challenge myself to using minimal space.


回答1:


Well theoretically you could do something like this, if both variables are already defined:

var1, var2 = (value, var2) if booleanCheck() else (var1, value)

Not saying that you should do it, though.




回答2:


Since the use case is assigning to the correct side of a binary tree, I assume the assignment targets are actually attributes of some tree node, and the usual code would be

if condition:
    node.left = value
else
    node.right = value

This code could be replaced by

setattr(node, "left" if condition else "right", value)

or alternatively

vars(node)["left" if condition else "right"] = value

It could be replaced by this, but it really shouldn't.




回答3:


It's not very good form to use locals or globals in this way (confusing, not often used, hard to debug, terrible performance), but it can be done:

varname = 'a' if some_condition else 'b'
locals()[varname] = value # or globals()
print locals()[varname] # or globals()

I think the real challenge here is to resist seemingly "fancy" or "creative" code and write code that others will understand immediately. Not to mention yourself if you have to come back to it later. But it is worth it to know what funky code (as in old cheese) looks like.




回答4:


Unlike locals(), globals() is documented to be updateable (as it returns the __dict__ of the current module). I routinely use vars() in the global scope - which is the same in that case - to reduce code duplication:

for v,ar in ('a',x),('b',y): vars()[v]=fn(ar)

For some others objects (like the current class/instance), __dict__'s members are accessible as syntactic variables, too:

class C:
    def __init__(self,a,b):
        vars(self).update(v,locals()[v] for v in 'a','b')
        do_something(self.a)

Do note, however, that:

  • These "syntactic references" reduce code maintainability - as they are harder to find - and thus will likely upset code checking tools like pylint.
  • a need to use variables in such a way typically signifies an attempt to use a scope as a dict: such variables tend to be "homogenous" in some way, asking for being used in array operations, and thus should probably be combined into a dedicated container instead.
    • this goes double when variable names are autogenerated - and also runs the risk of collisions. See also How can you dynamically create variables via a while loop? on this.


来源:https://stackoverflow.com/questions/17835014/can-i-conditionally-choose-what-variable-i-assign-a-value-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!