RFC3261 python源码分析 3

今天开始进入python的对象世界。


class Header(object):
    '''A SIP header object with dynamic properties.
    Attributes such as name, and various parameters can be accessed on the object.

    >>> print repr(Header('"Kundan Singh" <sip:kundan@example.net>', 'To'))
    To: "Kundan Singh" <sip:kundan@example.net>
    >>> print repr(Header('"Kundan"<sip:kundan99@example.net>', 'To'))
    To: "Kundan" <sip:kundan99@example.net>
    >>> print repr(Header('Sanjay <sip:sanjayc77@example.net>', 'fRoM'))
    From: "Sanjay" <sip:sanjayc77@example.net>
    >>> print repr(Header('application/sdp', 'conTenT-tyPe'))
    Content-Type: application/sdp
    >>> print repr(Header('presence; param=value;param2=another', 'Event'))
    Event: presence;param=value;param2=another
    >>> print repr(Header('78  INVITE', 'CSeq'))
    CSeq: 78 INVITE
    '''
以上代码定义了一个类Header。

    def __init__(self, value=None, name=None):
        '''Construct a Header from optional value and optional name.'''
        self.name = name and _canon(name.strip()) or None
        self.value = self._parse(value.strip(), self.name and self.name.lower() 

__init__函数类似C++的构造函数,在创建对象时会自动调用。类的成员函数第一个参数必须是self, 类似C++的this指针,但this指针并不需要显式指定。
成员函数的参数可以指定默认参数,"None"是python定义的表示空的对象。


在类和成员函数定义下面出现的三引号会自动文档化,比如下面的例子:
>>> class C():
"""class C"""
def mf():
"""C::mf"""
return

>>> help(C)
Help on class C in module __main__:


class C
 |  class C
 |  
 |  Methods defined here:
 |  
 |  mf()
 |      C::mf


>>> print(C.__doc__)
class C
>>> print(C.mf.__doc__)
C::mf
>>> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值