python中lns是什么意思_Python __builtins__模块拾穗

1.isinstance函数:除了以一个类型作为参数,还可以以一个类型元组作为参数。

isinstance(obj,basestring)===isinstance(obj,(str,unicode))

2.getattr函数:可以给一个默认值,以免触发错误。

write=getattr(obj,'write',sys.stdout.write)

3.type函数:即可以得到一个对象的类型,也可以直接由它创建一个新类型:

1 >>> Point=type('Point',(object,),{'x':0,'y':0})2 >>> p=Point()3 >>>p.x,p.y4 (0, 0)5 >>> p=Point(3,8)6

7 Traceback (most recent call last):8 File "", line 1, in

9 p=Point(3,8)10 TypeError: object() takes no parameters11 >>>pprint.pprint(dir(Point))12 ['__class__',13 '__delattr__',14 '__dict__',15 '__doc__',16 '__format__',17 '__getattribute__',18 '__hash__',19 '__init__',20 '__module__',21 '__new__',22 '__reduce__',23 '__reduce_ex__',24 '__repr__',25 '__setattr__',26 '__sizeof__',27 '__str__',28 '__subclasshook__',29 '__weakref__',30 'x',31 'y']32 >>> p.name='source point'

33 >>>p.name34 'source point'

35 >>>pprint.pprint(dir(p))36 ['__class__',37 '__delattr__',38 '__dict__',39 '__doc__',40 '__format__',41 '__getattribute__',42 '__hash__',43 '__init__',44 '__module__',45 '__new__',46 '__reduce__',47 '__reduce_ex__',48 '__repr__',49 '__setattr__',50 '__sizeof__',51 '__str__',52 '__subclasshook__',53 '__weakref__',54 'name',55 'x',56 'y']57 >>> deftostr(self):58 return '(%s,%s)'%(self.x,self.y)59

60 >>> Point.__str__=tostr61 >>> printp62 (0,0)63 >>> definit(self,x,y):64 self.x,self.y=x,y65

66

67 >>> Point.__init__=init68 >>> p2=Point(6,8)69 >>> printp270 (6,8)71 >>>

4.issubclass(bool,int)==True

5.numbers.Number是所有数字类型的基类

6.type(None)==NoneType,None是一个常量

7.iter函数除了iter(object)形式,还有iter(callable,sentinel)也是返回一个iterator对象

1 >>> defgetrand():2 importrandom3 return random.randint(1,100)4

5 >>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数

6

7 32 19 82 28 30 41 100 39 71 29 45 30 94 77 62 26 25 19 82 20 55 20 43 73

8 >>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数

9

10 22 54 14 25 60 65 16 80 61 5 48 61 2 30 90 98 70 10 55 45 23 72 87 39 70 3 84 85

11 >>>

8.BaseException是一切exceptions的基类,Exception只是一切不exit的exceptions的基类

9.locals/globals/vars/dir:

[1]locals/globals很简单,是相对于当前作用域的本地/全局对象dict;

[2]vars()==locals(),vars(obj)==obj.__dict__

[3]没有参数,set(dir())==set(locals().keys());if hasattr(obj,'__dir__')=>dir(obj)==obj.__dir__();否则,如果obj是模块对象,dir(obj)返回的是模块的所有属性;如果obj是类对象,dir(obj)返回的是类的所有属性,然后是从基类继承来的属性;如果obj是实例对象,dir(obj)返回的是实例对象专有的属性、其所属类的属性、其所属类基类继承来的属性。【对类对象的任何修改,必将反映到其实例对象上;对基类的任何修改,也必将反映到派生类上。当然,属性遮蔽的情况除外。】

10.enumerate函数:enumerate(obj,[start]),如果定义了start,则序数将从start开始,而不是从默认的零开始。

>>> for i,name in enumerate(['C','C++','CSharp','Java','Python'],1):print '%d.%s'%(i,name)1.C2.C++

3.CSharp4.Java5.Python>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值