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)

Answers


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.


Need Your Help

Get records from json store extjs

javascript json extjs store

I have a json store loaded, I need to grab one record from it.

How can I instruct the parser not to continue processing unterminated comments?

bison yacc flex-lexer

I'm working on improving error reporting on my compiler assignment. I'm handling unterminated comments in Flex using the following code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.