How to Declare Classes : self « Class « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Class » self 
11.7.1.How to Declare Classes
# __init__() is a constructor.
# self is an instance's handle to itself. 
# Other OO languages often use an identifier called this.

class ClassName (base_class[es]):
#   static_member_declarations
#   method_declarations


class FooClass(object):
    version = 0.1              
    def __init__(self, nm='A'):
           self.name = nm      
           print'Created a class instance for', nm
    def showname(self):
           print 'Your name is', self.name
           print 'My name is', self.__class__.__name__
    def showver(self):
           print self.version  
    def addMe2Me(self, x):     
           return x + x
           
foo1 = FooClass()

foo1.showname()
foo1.showver()
print foo1.addMe2Me(5)
print foo1.addMe2Me('xyz')
11.7.self
11.7.1.How to Declare Classes
11.7.2.self.__class__.__name__ represents the name of the class
11.7.3.Use self to reference properties
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.