Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类方法中未解析的引用_Python_Function_Scope - Fatal编程技术网

Python 类方法中未解析的引用

Python 类方法中未解析的引用,python,function,scope,Python,Function,Scope,我不认为这是因为函数的范围,但我得到了一个 get\u all\u谓词处未解析的引用(示例)。计数(谓词\u列表[0]) 在我的类树中的函数中获取属性的\u熵(示例,谓词列表): class Tree: def get_examples(examples, attributes): for value in examples: yield dict(zip(attributes, value.strip().replace(" ", "").sp

我不认为这是因为函数的范围,但我得到了一个

get\u all\u谓词处未解析的引用(示例)。计数(谓词\u列表[0])

在我的类
树中的
函数中
获取属性的\u熵(示例,谓词列表)

class Tree:

    def get_examples(examples, attributes):
        for value in examples:
            yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))

    def get_all_predicates(examples):
        return [d['Predicate'] for d in examples]

    def get_entropy_of_attributes(examples, predicate_list):
        get_all_predicates(examples).count(predicate_list[0])
        return 0

    examples = list(get_examples(all_examples, name_of_attributes))

    predicate_list = list(set(get_all_predicates(examples)))

    get_entropy_of_attributes(examples, predicate_list)
all_examples
是字典列表,而
name_of_attributes
是保存从文本文件导入的值的列表

all_examples = [{'P_Length': '1.4', 'P_Width': '0.2', 'Predicate': 'I-setosa', 'Sepal_Width': '3.5', 'S_Length': '5.1'}, ...]

name_of_attributes = ["Check","P-Width"]

有什么帮助吗?

如果你想调用类方法,你必须用
self
调用它们,例如

class myClass:

    def __init__(self):
        pass

    def get_all_predicates(self):
        print('asd')

    def do_something(self):
        self.get_all_predicates()  # working
        get_all_predicates()  # → Unresolved reference

test = myClass()
test.do_something()

有关Python类的示例,请参见。

类没有作用域,只有名称空间。这意味着其中定义的函数无法自动查看其他类变量

class Foo(object):
    var = 1              # lets create a class variable
    def foo():
        print(var)       # this doesn't work!
要访问类变量,您需要使用属性语法:
Foo.var
(通过类访问),或者,如果您正在编写实例方法,则使用
self.var
(通过当前实例访问,它将作为第一个参数传入)

通过这种设置,您几乎可以修复当前代码(但不是完全修复)

如果在类完全初始化后调用它,它将毫无例外地工作(尽管它的实现似乎有点荒谬)。然而,当您调用它来定义一个类变量时,它不会像您当前的代码那样工作。这是因为只有在运行了所有的类主体之后,才会创建类对象并将其绑定到类名


我认为解决这个问题的办法可能是以更传统的方式重新设计类。与基于各种全局变量(如
all\u examples
)设置类变量不同,您可能应该通过将参数传递给构造函数并使其他变量成为实例属性来创建类实例。我想把它写出来,但坦率地说,我不明白你做得够好。

什么是
所有的例子
属性的名称
?你能给我一个
所有例子
属性的名称
?我不能让你的代码运行,因此不能重现错误。您是否可以尝试通过在代码示例中包含
所有\u示例
属性名称
来给出代码的一个运行示例?请尝试给出一个导致错误的运行示例。创建一个新文件,复制其中的示例行,并相应地编辑您的问题。@bastelflp刚刚再次进行了更改。
class Bar(object):
    var = 1
    def bar1():
        print(Bar.var) # works
    def bar2(self):
        print(self.var) # also works, if called on an instance, e.g. `Bar().bar2()`
def get_entropy_of_attributes(examples, predicate_list):
    Tree.get_all_predicates(examples).count(predicate_list[0])     # name the class
    return 0