Understanding Python classes
I am trying to learn about python classes, but I don't understand something. Why does this simple example not return "6"? It returns <function TEST.f at 0x00000000029ED378> instead. I have also tried TEST.f() but then he tells me that the argument self is missing. Shouldn't self only exist inside the class and python fills it in automatically?
#! coding=utf-8 class TEST: def __init__(self): self.x = 6 def f(self): return(self.x) print(TEST.f)
You need to create an instance of the class.
test = TEST() print test.x()
But you also need to call the method and the variable different things.
class TEST: def __init__(self): self._x = 6 def x(self): return(self._x)
Otherwise you're redefining the value of x.