[英]How do you print the variable name when the variable points to an object in Python
I'm new to python and coding in general.我是 python 和一般编码的新手。 In the code below how do I get "turn" to print "june" or "july" instead of <turtle.Turtle object at 0x0405D730>
在下面的代码中,我如何让“turn”打印“june”或“july”而不是 <turtle.Turtle object at 0x0405D730>
def main():
wn = turtle.Screen() #Creates a screen
#Define your turtles here
june = turtle.Turtle()
july = turtle.Turtle()
june.shape('turtle')
july.shape('turtle')
june.color('blue')
july.color('red')
july.goto(0, 50) #move second turtle to a different starting location
turtleTurn()
turn = turtleTurn()
if turn == 0:
turn = june
else:
turn = july
while isInScreen(wn, turn) and sameSpot(june, july):
if turn == june:
turn = july
else:
turn = june
turtleMove(turn)
if isInScreen(wn, turn) == False:
print("and the winning is ", turn)
wn.exitonclick()
main()
You can just store a name in each Turtle (after creating it) and refer to the name attribute to print it:您可以在每个 Turtle 中存储一个名称(在创建它之后)并参考 name 属性来打印它:
june.name = 'june'
july.name = 'july'
...
print("and the winning is ", turn.name)
You can do this in many ways, and one of them is this:您可以通过多种方式做到这一点,其中之一是:
my_var = "sss"
my_var_name = [ k for k,v in locals().items() if v == my_var][0]
print("Variable name is : ", my_var_name)
Here we create a virable "my_var", when we declare a virable he is saved in local virables and u can access them by using "locals().items()" which return all of them.在这里,我们创建了一个病毒“my_var”,当我们声明一个病毒时,他被保存在本地病毒中,你可以使用“locals().items()”来访问它们,它会返回所有这些。 And by the for you iterate on them and when u find the v == to the virable u get it in "my_var_name".
并且通过 for you 对它们进行迭代,当你找到病毒的 v == 时,你会在“my_var_name”中得到它。
You can't get the variable name itself, at least not easily.你不能得到变量名本身,至少不容易。 See the explanation here: Getting the name of a variable as a string -- kindall's answer
请参阅此处的说明:将变量的名称作为字符串获取——kindall 的回答
Instead you can compare the object identity and print its name manually:相反,您可以比较 object 身份并手动打印其名称:
print("and the winning is", 'july' if turn is july else 'june')
Though for what it's worth, the most generic solution is to use a dict.尽管物有所值,但最通用的解决方案是使用字典。 See How do I create a variable number of variables?
请参阅如何创建可变数量的变量? For example, starting with something like this:
例如,从这样的事情开始:
turtles = {month: turtle.Turtle() for month in ['june', 'july']}
You just need to wrap june
and july
with the value wrapper from python-varname
:您只需要使用来自
python-varname
的值包装器来包装june
和july
月:
from varname import Wrapper
def main():
wn = turtle.Screen() #Creates a screen
#Define your turtles here
june = Wrapper(turtle.Turtle()) # <- then, use june.value to access turtle
july = Wrapper(turtle.Turtle()) # <- and june/july.name to access the name
june.value.shape('turtle')
july.value.shape('turtle')
june.value.color('blue')
july.value.color('red')
july.value.goto(0, 50) #move second turtle to a different starting location
turtleTurn()
turn = turtleTurn()
if turn == 0:
turn = june
else:
turn = july
while isInScreen(wn, turn.value) and sameSpot(june.value, july.value):
if turn.value == june.value:
turn = july
else:
turn = june
turtleMove(turn.value)
if isInScreen(wn, turn.value) == False:
print("and the winning is ", turn.name)
wn.exitonclick()
main()
The package is hosted at https://github.com/pwwang/python-varname . package 托管在https://github.com/pwwang/python-varname上。
I am the author of the package.我是 package 的作者。 Let me know if you have any questions using it.
如果您对使用它有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.