Sunday, December 19, 2010

Classes In Python

This is a description of Python class:

>> class Data:
>>    def __init__(self,fname,age):
>>         this.fname = fname
>>         this.age = age
>>    lname = "no defined last name"
>>    def setFName(self,fname):
>>        this.fname = fname
>>    def setAge(self, age):
>>        this.age = age
>>    def getFName(self):
>>        return this.fname
>>    def getAge(self):
>>        return this.age

To instantiate an object of class Data, use:
>> lady = Data("Lady",24);
>> print lady.getFName()
>> print lady.getAge()
>> print lady.lname

Always use bracket when instantiate an object of a class, although there's no argument in the constructor. For data, no use of "self". For method, always "self" as the first argument in function declaration to allow it accessed from outside.

No comments:

Post a Comment