简体   繁体   English

当变量指向 Python 中的 object 时,如何打印变量名

[英]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的值包装器来包装junejuly月:

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.

相关问题 Python - 如何打印 Object 的变量名 - Python - How to print the variable name of an Object 在python django中,如何打印出对象的内省? 该对象的所有公共方法列表(变量和/或函数)? - In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)? 你如何在python的循环中改变变量名? - How do you alter a variable name in a loop in python? Python-如何使用getattr调用变量对象? - Python - How do you use getattr to call an variable object? 你如何检查一个变量是否引用了另一个在 Python 中声明的 object? - How do you check if a variable references another declared object in Python? 使用变量实例化时如何调用特定对象? - How do you recall a specific object when using a variable to instantiate? 如何从 python 中的 *args 打印变量名 - how to print variable name from *args in python 您是否可以将if语句分配为变量,以及在计算时如何打印if语句的结果 - Can you assign an if statement as a variable and how do you print the result from an if statement when it is a calculation 在Python中切割字符串时,如何使用变量作为索引? - How do you use a variable as an index when slicing strings in Python? 在 Python 中切片整数时如何使用变量作为索引? - How do you use a variable as an index when slicing integers in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM