chap Class

>>> john = AddrBookEntry('John Doe', '408-555-1212')
Created instance for: John Doe
>>> jane = AddrBookEntry('Jane Doe', '650-555-1212')
Created instance for: Jane Doe

 

Class MyData(object):
    pass

>>> class MyData(object):
...   pass
...
>>> mathObj = MyData()
>>> mathObj.x = 4
>>> mathObj.y = 5
>>> mathObj.x + mathObj.y
9

  

"x" and "y" belong to the mathObj, the mathObj act as a container.

Not of class MyData


 

self is like the C++ this key word.

this is the python philosophy, everything declared explicitly


 

__init__() is like the constructor

Python creates the instance for you and then calls __init__() during instantiation

to define additional behavior that should occur when a class is instanciated.

classAddrBookEntry(object): # class definition
    'address book entry class'
    def __init__(self, nm, ph): # define constructor
        self.name = nm # set name
        self.phone = ph # set phone#
        print'Created instance for:', self.name
    def updatePhone(self, newph): # define method
        self.phone = newph
        print'Updated phone# for:', self.nam

  

The only initialize the parameter is the name and phone that is --nm --ph

 

 

 

转载于:https://www.cnblogs.com/geometry-/archive/2012/06/28/2568737.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值