目录
1、type
语法:
type(object)
type(name, bases, dict)
name:类的名称;
bases:基类的元组;
dict:字典,类内定义的命名空间变量;
作用 / 返回值:
若只有一个参数,返回对象的类型;
三个参数时,返回新的参数对象;
代码演示:
print('1、', type(1))
print('2、', type('1'))
print('3、', type([1]))
print('4、', type({1:'a'}))
print('5、', type(1) == int)
class A(object):
a = 1
B = type('A', (object, ), dict(a = 1))
print(B)
type()与isinstance()的区别如下:
print('1、', isinstance(1, int))
print('2、', isinstance(1, str))
print('3、', isinstance(1, (str, int, tuple)))
print('4、', isinstance((1, 2), (str, int, tuple)))
class A:
pass
class B(A):
pass
print('5、', isinstance(A(), A))
print('6、', isinstance(B(), A))
print('7、', type(A()) == A)
print('8、', type(B()) == A)
2、bytearray
语法:
class bytearray([source[, encoding[, errors]]])
作用 / 返回值:
返回一个新字节数组;
返回的数组中元素时可变的,每个元素值得范围时 [0,256);
若 source 为整数,则返回一个长度为 source 的初始化数组;
若 source 为字符串,按照指定的 encoding 将字符串转换为字节序列;
若 source 为可迭代类型,则元素必须为 [0,255] 中的整数;
若 source 为与 buffer 接口一致的对象,则此对象也可被用于初始化 bytearray;
若无任何输入参数,则默认初始化数组为0个元素;
代码演示:
print('1、', bytearray())
print('2、', bytearray(1))
print('3、', bytearray([1, 2, 3]))
print('4、', bytearray('123', 'utf-8'))
3、vars
语法:
vars([object])
object:对象;
作用 / 返回值:
返回对象 object 的属性和属性值的字典对象;
如果没有参数,就打印当前调用位置的属性和属性值,类似locals;
代码演示:
print('1、', vars())
class A:
a = 1
print('2、', vars(A))
B = A()
print('3、', vars(B))
4、classmethod
语法:
classmethod
作用 / 返回值:
classmethod 修饰符对应的函数不需要实例化;
不需要self参数,但是第一个参数需要是表示自身类的cls参数,可以来调用类的属性、类的方法、实例化对象等;
返回函数的类方法;
代码演示:
class A:
bar = 1
def func1(self):
print('func1')
@classmethod
def func2(cls):
print('func2')
print(cls.bar)
cls.func1(0)
A.func2()
5、callable
语法:
callable(object)
object:对象;
作用 / 返回值:
用于检查一个对象是否是可调用的;
若返回True,仍可能调用失败;但返回False,必不可调用;
对于函数、方法、lambda、类、实现了__call__方法的类实例,都返回Ture;
代码演示:
print('1、', callable(0))
print('2、', callable('0'))
def add(a, b):
return a + b
print('3、', callable(add))
class A:
def func(self):
return 1
print('4、', callable(A))
a = A()
print('5、', callable(a))
class B:
def __call__(self):
return 2
print('6、', callable(B))
b = B()
print('7、', callable(b))
6、format
语法:
str.format()
作用 / 返回值:
一种格式化字符串的函数;
使用 {} 和 : 代替 % ;
可以使用大括号{}来转义大括号;
可以接收多个参数,位置可以不按顺序;
格式 | 描述 |
---|---|
{:.2f} | 保留小数点后两位; |
{:+.2f} | 带符号保留小数点后两位; |
{:.0f} | 不带小数(四舍五入); |
{:0>2d} | 数字补零(填充左边,宽度为2); |
{:x<4d} | 数字补x(填充右边,宽度为4); |
{:,} | 以逗号分隔的数字形式; |
{:.2%} | 百分比格式(保留两位小数); |
{:.2e} | 指数记法; |
{:>10d} | 右对齐(默认宽度为10); |
{:<10d} | 左对齐(宽度为10); |
{:^10d} | 中间对齐(宽度为10); |
{:b} | 变为二进制数; |
{ : o } | 变为八进制数; |
{:d} | 变为十进制数; |
{:x} | 变为十六进制数; |
{:#x} | 变为十六进制数(0x_); |
代码演示:
print('1、{} 的学号是{{001}}'.format('zhangsan'))
print('2、{:.3f}'.format(3.1415926))
print('3、{} {}'.format('A', 'B'))
print('4、{1} {0} {1}'.format('A', 'B'))
a_list = ['a', 'b', 'c']
print('5、{}'.format(a_list))
print('6、{0[1]} {0[2]}'.format(a_list))
b_dict = {'a': 'hello', 'b': 'python'}
print('7、{b}'.format(**b_dict))
class A():
def __init__(self, x):
self.n = x
a = A(102)
print('8、{0.n}'.format(a))
7、locals
语法:
locals()
作用 / 返回值:
以字典形式返回当前位置的全部局部变量;
对于函数、方法、lambda、类、实现了__call__方法的类实例,都返回True;
代码演示:
def func(n):
a = 1
print(locals())
func(2)
8、reduce
语法:
reduce(function, iterable[, initializer])
function:有两个参数的函数;
iterable:可迭代对象;
initializer:初始参数(可选);
作用 / 返回值:
对参数序列中元素进行累积;
将对数据集合(iterable)中所有数据进行如下操作:先将 iterable 集合中的第1、2个元素传入 function 进行运算得出结果,再将此结果与 iterable 集合中的第3个元素传入 function 进行运算,直到最后;
返回最终计算结果;
代码演示:
# python3中reduce函数转移到了functools模块中
from functools import reduce
def func(x, y):
return x * y
print('1、', reduce(func, [1, 2, 3, 4]))
print('2、', reduce(lambda x, y: x * y, [1, 2, 3, 4]))
9、getattr
语法:
getattr(object, name[, default])
object:对象;
name:字符串,对象属性;
default:默认返回值(若不提供该参数,在没有对应属性时,出发AttributeError);
作用 / 返回值:
返回对象属性值;
代码演示:
class A:
x = 1
a = A()
print('1、', getattr(a, 'x'))
print('3、', getattr(a, 'y', 2))
10、frozenset
语法:
class frozenset([iterable])
iterable:可迭代对象;
作用 / 返回值:
返回 一个冻结的集合,冻结后集合不能再添加或删除任何元素;
若不提供任何参数,会生成空集合;
代码演示:
print(frozenset(range(1, 16, 3)))
print(frozenset('aaabbbcccddd'))