python3.6library 学习 1.introduction,2.built-infunction

介绍

这一小节的链接https://docs.python.org/3.6/library/intro.html
简单介绍了python。
1,包含了不需要import的对象——内置的函数和exceptions
2,有些module是C编写的,内置在解释器里,因此在看其函数时会发现空的
3,python中对不同操作系统是不一样的,这点要注意,有些函数在Windows和unix是不一样
4,还有一些必须在安装python时选择特定的安装配置

内置函数

这一小节的链接https://docs.python.org/3.6/library/functions.html
内置函数较多,大多数仅仅简单介绍。
下图为列举了内置函数按首字母排列。
在这里插入图片描述
abs(x):参数x为整形或浮点型,返回绝对值;x为复数,返回复数的模。
,
,
,
all(iterable):参数为可迭代的,其所有元素为True返回布尔值True,否则为False。iterable的定义,这种继承了抽象类Iterable,有抽象方法__iter__。下图简单列举了Iterable的子类树状图(后面还有不列举了)。具体参考内置类型和数据类型;当iterable内无元素,返回true。
在这里插入图片描述
,
,
,
any(iterable):类比all(),输入有true,返回true;空,返回false。
,
,
,
ascii(object):原文解释,输入object,返回一个包含客打印代表这个objext的字符串,如repr()相似,但对non-ASCII字符会溢出(或者说是转义)。
下面对object分类介绍:

输入 输出
module <module ‘drawtree’ from ‘C:\Users\Administrator\PycharmProjects\untitled1\tool\drawtree.py’>
class <class ‘collections.abc.Iterable’>
def <function gettreedic at 0x00000000025A8BF8>
实例化对象 <main.Lqq object at 0x00000000025C10B8>

大概总结一下:都会返回这个object的类型如module、class、function等;module会返回这个文件的绝对路径,function和实例化的对象(就是class的具体对象还会返回这个对象的数据储存地点)。
最后说一下ascii和repr的不同后面就不提了,代码如下,acii(American Standard Code for Information Interchange)不包含汉字,只能利用转义字符\u来打破限制又称escape。

a = '了圣诞节疯狂'
ascii(a)
"'\\u4e86\\u5723\\u8bde\\u8282\\u75af\\u72c2'"
repr(a)

“‘了圣诞节疯狂’”
,
,
,

repr(object):上面ascii也解释了以下,对于repr,一个类可以用通过重写__repr__()控制这函数的输出。
,
,
,
dir([object1]):当无,返回现在的局部变量名;有参数,返回他的有关特性。
如果参数object1内重写了object__dir__().执行dir,如果没有,则执行object.dir()。
规则如下:
If the object is a module object, the list contains the names of the module’s attributes.
If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

class Person:
    def __dir__(self):
        return (['dfdf'])
person =Person()
print(dir(person))#dfdf

,
,
,
help([object]):和dir一样本是为交互模式设计的。
object是类则会返回紧接着class声明的三引号注释,class的方法,class的__dict__(实例变量)和__weakref__(弱引用)(help不显示内容),class的变量和其他属性。
object是独立的函数,返回def前的注释。等不在赘述。

class Person:
    a=1
    def __init__(self, age):
        self.age = age
    def __eq__(self, other):
        if not isinstance(other, Person):
            print('第一次')
            return NotImplemented
        return self.age == other.age

import weakref
person =Person(1)
a=weakref.ref(person)
print(help(person))
print((person.__weakref__()))
print(vars(person))
print(vars(person)==person.__dict__)
以下是返回,可见vars(ob)==ob.__dict__,help不详细显示dict和weakref的内容
Help on Person in module __main__ object:

class Person(builtins.object)
 |  Methods defined here:
 |  
 |  __eq__(self, other)
 |      Return self==value.
 |  
 |  __init__(self, age)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
  |  a = 1

None
<__main__.Person object at 0x0000000002571160>
{'age': 1}
True

,
,
,
vars([object]):返回一个module,class,isntance或者其他有_dict__属性的对象。
如module和instance的__dict__是可更新的,但其他对象可能会限制,如类用types.MappingProxyType来阻止更新。
,
,
,

ord(ch):输入一个代表unicode的str,返回一个int;char与之相反。
,
,
,
char(int):与ord相反。
,
,
,
bin(x):输入为整型,返回他的字符串二进制并用“ob”当前缀如(bin(3)—‘ob11’);输入的不是整型,必须满足x有一个返回整型的__index__()方法。这种函数特别多,因此下面给一个例子以后不再赘述。

class Lqq:
    def __index__(self):
        return 1
if __name__ == "__main__":
#这里的__name__,你会发现是有值的,规则如下
#当这个module是被执行的文件时,值为__main__;
#而如果不是(如只是被import的),值为module的名即不带后缀的py文件。
#加了这句话,就可以防止,import时,运行被import的module了。
    a = Lqq()
    print(bin(a))

结果为1。
,
,
,
oct(x):前缀‘0o’
,
,
,
hex(x):输入int,返回一个前缀为’ox’的int的16进制的字符串;不是int则同

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值