When to use self to reference class itself.

Use self

The keyword self is used to reference the class itself.


class HideX(object):
    def __init__(self, x):
        self.x = x#   w  ww .j  ava2s  . c o  m

    def get_x(self):
        return ~self.__x

    def set_x(self, x):
        assert isinstance(x, int), '"x" must be an integer!'
        self.__x = ~x

    x = property(get_x, set_x)

inst = HideX(20)
print inst.x
inst.x = 30
print inst.x

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules