Reputation: 35
I am sorry but i am just starting with python, but i had a Unresolved reference get_func() error in the following code:
class Foo:
fo = open(file_name, "r")
with open(file_name, 'r') as file:
examples = int(file.readline())
attributes = int(file.readline())
name_of_attributes = [n for n in (file.readline().replace(" ", "")).split(",")]
all_examples = file.readlines()
get_func(); // error here
def get_func(self):
list_of_keys = ['S_Length', 'S_Width', 'P_Length', 'P_Width', 'Predicate']
with open('example.txt') as f:
for line in f:
return;
Upvotes: 1
Views: 593
Reputation: 6834
To access a class method you should use the self
attribute you have set in the method get_func
declaration, like this self.get_func()
. You can abandon the colon at the end.
I think you should be reading more about classes in Python, check out this Wikibook article - Python beginner's tutorial to classes
Upvotes: 0
Reputation: 76297
At the point where you're invoking the function, Python hasn't yet encountered its definition, so it doesn't yet know to what you're referring. In C/C++ (judging from your in-code comment), there's a clear distinction between compiling the code and then running it. In Python, the interpreter conceptually just interprets it as it goes along (there's bytecode compilation, but that's besides the point).
Try changing the order so that the invocation is after the definition:
def get_func(self): # first define
...
get_func() # now invoke it
Upvotes: 1