简体   繁体   English

变量名称要在python函数中分配为参数

[英]Variable name to be assigned as argument in a python function

I am writing a function that builds and populates a tree-like data structure, using the treelib library. 我正在编写一个使用treelib库构建并填充树状数据结构的函数。 You create a tree as follows: 您创建树如下:

foo = Tree()

...and go from there, which is standard enough. ...然后从那里去,这已经足够标准了。 Here is what I have, simplified: 这是我的简化内容:

def make_special_tree(tree, arg1, arg2):
    tree = Tree()
    other_stuff = things(arg1)
    modify_tree(tree, other_stuff, arg2)
    return tree

Here's the thing: let's say that I ultimately want a tree object called blah . 事情就是这样:假设我最终想要一个叫做blah的树对象。 If I do the following, the command runs without an error: 如果执行以下操作,则命令运行无错误:

make_special_tree('blah', foo, bar)

...but when I type blah afterward, I get back NameError: name 'blah' is not defined . ...但是当我之后输入blah ,我得到NameError: name 'blah' is not defined If I do the following, the command also runs without an error: 如果执行以下操作,该命令也将运行且没有错误:

blah = make_special_tree('yoink', foo, bar)

...and when I type blah afterward, I get back <treelib.tree.Tree object at 0x10e60db10> , which is what I want to see. ...然后当我键入blah ,我得到<treelib.tree.Tree object at 0x10e60db10> ,这是我想要看到的。 yoink meanwhile remains undefined, like blah in the previous version. 同时, yoink仍未定义,就像先前版本中的blah一样。

Hence my question--and I can tell this is basic, but I cannot untangle this, in part because I am not sure how precisely to ask the question. 因此,我的问题-我可以说这是基本的,但是我不能解开这个问题,部分原因是我不确定提出问题的精确程度。 As you can see, right now I have to create an instance of class Tree() and I think I have to feed my function an argument to do so. 如您所见,现在我必须创建类Tree()的实例,并且我必须为此函数提供一个参数。 I think that blah = make_special_tree(args) is the correct way to format this, but how can I pass the variable blah as the name of the tree structure I'd like returned? 我认为blah = make_special_tree(args)是格式化此格式的正确方法,但是如何传递变量blah作为我想返回的树结构的名称呢?

The first argument in your function is redundant. 函数中的第一个参数是多余的。 Try this: 尝试这个:

def make_special_tree(arg1, arg2):
    tree = Tree()
    other_stuff = things(arg1)
    modify_tree(tree, other_stuff, arg2)
    return tree

blah = make_special_tree(foo, bar)

Python functions generally don't "assign variable names", they just return values, and you can do whatever you want with those values. Python函数通常不“分配变量名”,它们只是返回值,您可以使用这些值做任何您想做的事情。

A value doesn't have a variable name. 值没有变量名。 * Because a value can be stored in five different variables, or it can be stored in things that aren't variables, like the elements of a list. *因为值可以存储在五个不同的变量中,也可以存储在非变量的事物中,例如列表的元素。 It doesn't care. 不在乎。

So, what you almost certainly want to do is what @spectras suggested in a comment: Get rid of that first parameter, and then store the value in whatever variable you want: 因此,您几乎可以肯定想做的是@spectras在注释中建议的内容:删除第一个参数,然后将值存储在所需的任何变量中:

def make_special_tree(arg1, arg2):
    tree = Tree()
    other_stuff = things(arg1)
    modify_tree(tree, other_stuff, arg2)
    return tree

# Two different trees with different names
blah = make_special_tree(foo, bar)
yoink = make_special_tree(foo, bar)

# One tree with two names
spam = make_special_tree(foo, bar)
eggs = spam

# A tree with no names at all
cheese = []
cheese.append(make_special_tree(foo, bar))

If you really do want to create a global variable with a specified name, you can do that, ** but it's a really bad idea, unless you're trying to do something unusual, like the way collections.namedtuple acts like a class statement. 如果确实要创建具有指定名称的全局变量,则可以执行此操作, **但这是一个非常糟糕的主意,除非您尝试执行一些不寻常的操作,例如collections.namedtuple行为类似于class语句。


* A value can have a name by having, eg, a name attribute. *值可以具有名称,例如具有name属性。 You can even change the definition of the Tree class to use that name attribute as part of its output representation. 您甚至可以更改Tree类的定义,以将该name属性用作其输出表示的一部分。 But this still has nothing to do with what variables you store it in. 但这仍然与存储它的变量无关。

** The least hacky way is globals()[name] = value . **最少的方法是globals()[name] = value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM