How to create an use properties in Python class

Define property

Attributes are values associated with the object.

Methods are callable functions that perform an operation on the object. Attributes and methods of an object can be accessed using the dot '.' syntax:


class test(object):
   def printNum(self):
      print self.num
#  w  ww.jav a2 s  .co  m
t = test()
t.num = 4
t.printNum()

The code above generates the following result.

setattr and getattr

  • __getattribute__(self, name): Automatically called when the attribute name is accessed.
  • __getattr__(self, name): Automatically called when the attribute name is accessed and the object has no such attribute.
  • __setattr__(self, name, value): Automatically called when an attempt is made to bind the attribute name to value.
  • __delattr__(self, name): Automatically called when an attempt is made to delete the attribute name.




















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules