• Python note


    python 包,模块,类

    self只有在类的方法中才会有,独立的函数或方法是不必带有self,非关键词

    1 class Person:
    2          def _init_(self,name):
    3                   self.name=name
    4          def sayhello(self):
    5                   print 'My name is:',self.name
    6 p=Person('Bill')
    7 print p

    install setuptools

    第一步:下载setuptools压缩包(本人安装的Python版本是3.3,所以下载的是3.4.zip):https://pypi.python.org/packages/source/s/setuptools/
    第二步:将下载的安装包解压缩:
    第三步:把第一步中下载的zip包,原样拷到上面的这个目录下,如下图
    第四步:在CMD下切换到此目录,并执行 python ez_setup.py,即可看到安装进程了!
    说明: 如果直接执行 python ez_setup.py会包含在线下载文件的步骤,因为网络原因容易出现问题,以上的方案可以实现离线的安装。

    ________________________________________________________________

    读前辈的笔记遇到个很坑的例子

    A namespace is a mapping from names to objects

    >>> class a:
        def __cmp__(self,other):
            if other > 0 : print 'other > 0' ;return -1
            elif other < 0 : print 'other < 0' ;return 1
            else : print 'other==0'; return 0

            
    >>>
    >>>
    >>>
    >>> o=a()
    >>>
    >>> if (o > 10) : print 'tt'

    other > 0
    >>> if (o < 10) : print 'tt'

    other > 0
    tt
    >>>
    >>>
    >>>
    >>>
    >>> if (o > -10) : print 'tt'

    other < 0
    tt
    >>> if (o < -10) : print 'tt'

    other < 0

    __cmp__ 中return的3个值 1,-1,0

    执行过程(猜测):

    1,根据other的值确定执行那一条,结果是返回1,-1,或0

    2,判断返回值是否和表达式中的符号匹配,匹配返回True,不匹配返回False

    ############################################################

     >>> a=eg_class()

    OK,I was born
    >>> Eg_class=eg_class
    >>> b=Eg_class()
    OK,I was born
    >>> a
    <__main__.eg_class instance at 0x0000000002E09EC8>
    >>> b
    <__main__.eg_class instance at 0x0000000002E0F9C8> // class seems like stucture

    object == class instance

    name space is the mapping of name and object 

    >>> a.new_att='a' or setattr(obj,"new_attribute",other)

    >>> a.__dict__     //name spaces of a
    {'new_att': 'a'}

    >>> a.__dict__.keys()   //name list
    ['new_att']

    >>> a.__dict__['new_att']
    'a'

    >>> del a.new_att  //...

    >>> hasattr(a,'new_att')
    True

    >>> dir(a)
    ['__doc__', '__init__', '__module__', 'new_att'] //list attribute of a 

    >>> vars(a)

    {'new_att': 'a'}
    >>> dir(a)
    ['__doc__', '__init__', '__module__', 'new_att']  // attribute not must in its name spaces
    >>>

    "*.py" create a moudle object

    >>> class eg_c:
    def __init__(self):
    print "class eg_c born"
    a='a'


    >>> class Eg_c(eg_c):   //继承
    def __init__(self):
    print "class Eg_c born"


    >>> dir(eg_c)
    ['__doc__', '__init__', '__module__', 'a']
    >>> dir(Eg_c)
    ['__doc__', '__init__', '__module__', 'a']
    >>> Eg_c.a
    'a'

    every class instance has obj.__class__ point to the class objetct

    >>> class A:
    ... count = 0
    ... def __init__(self):
    ... self.__class__.count= self.__class__.count + 1

    class A:
    readonlyValue=100
    def __setattr__(self,attr,value):
    if attr=="readonlyValue":
    raise "can not change the readonly value"

    a.readonlyValue = 102 //赋值会调用setaddr a=self attr=readonlyValue value=102

    -------------------------------------------------------

    getattr 类重载

    class B:
    def printMe(self):
    print "I am B"
    def printOk(self):
    print "B say:OK"

    class A:

    __realobj__ = B()
    def __getattr__(self,attr):
    return getattr(self.__realobj__,attr)
    def printMe(self):
    print "I am A"

    if __name__ =="__main__":
    a=A()
    a.__realobj__ = B() 
    a.printMe()     
    a.printOk() //本身没有printOK attr

    ------------------------------------------------------

    setaddr 动态增加attr

    class eg_c:

    def __init__(self,**kws):
    for k,v in kws.items():
    setattr(self,k,v)

    a=eg_c(five=5,six=6)

    >>> vars(a)   //{'six': 6, 'five': 5}

    >>> d={"one":1,"two":2,"three":3}
    >>> d.items()
    [('three', 3), ('two', 2), ('one', 1)]

    >>> for k,v in d.items():
    setattr(a,k,v)

    >>> vars(a)
    {'one': 1, 'six': 6, 'five': 5, 'three': 3, 'two': 2}

    >>> for k,v in vars(a).items(): // one:1 ...
    print "%10s:%s" % (k,v)

  • 相关阅读:
    各个地区2.4G及5G信道一览表 [转]
    nf_conntrack
    串口 属性 设置 termios.h [转载&修改]
    802.3 Logical Link Control 协议相关
    HTTP协议中GET、POST和HEAD的介绍[转载]
    Android Shell、SSH、GCC安装与配置
    mybatis中sql语句总结
    [leedcode 128] Longest Consecutive Sequence
    [leedcode 126] Word Ladder
    [leedcode 125] Valid Palindrome
  • 原文地址:https://www.cnblogs.com/eiguleo/p/3981959.html
Copyright © 2020-2023  润新知