Unresolved reference in class method

19,894

Solution 1

Classes do not have scopes, only namespaces. This means that functions defined within them cannot see other class variables automatically.

class Foo(object):
    var = 1              # lets create a class variable
    def foo():
        print(var)       # this doesn't work!

To access a class variable, you need use attribute syntax: either Foo.var (to access via the class) or, if you're writing an instance method, with self.var (to access via the current instance, which will be passed in as the first argument).

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()`

With this kind of setup you can almost fix your current code (but not quite).

def get_entropy_of_attributes(examples, predicate_list):
    Tree.get_all_predicates(examples).count(predicate_list[0])     # name the class
    return 0

If you call this after the class is fully initialized, it will work without any exceptions (though it's implementation seems a bit nonsensical). However, it doesn't work when you call it to define a class variable, as your current code does. That's because the class object is only created and bound to the class name after all of the class body has been run.

I think the fix for that is probably to redesign your class in a more conventional way. Rather than having class variables set up based on various globals (like all_examples), you should probably create instances of your class by passing in arguments to the constructor and making the other variables you calculate from them instance attributes. I'd try to write it out, but frankly I don't understand what you're doing well enough.

Solution 2

If you want to call class methods, you have to call them with self, e.g.

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()

See this link for examples for Python classes.

Share:
19,894
Clint Whaley
Author by

Clint Whaley

Updated on June 04, 2022

Comments

  • Clint Whaley
    Clint Whaley almost 2 years

    I don't think it is because of the scope of the function, but I get a

    Unresolved reference at get_all_predicates(examples).count(predicate_list[0])

    inside get_entropy_of_attributes(examples, predicate_list) function in my class Tree:

    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 is a list of dictionary and name_of_attributes is a list, that holds values imported from a text file.

    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"]
    

    Any help?