python的第一类对象的简易用法:
import math items ={ 'number':42, 'text':'Hello World' } items['func_abs'] = abs #内置绝对值函数 items['mod_math'] = math #内置模块 items['error'] = ValueError num = [1,2,3,4] items['append'] = num.append #添加对象的方法 print(items) print(items['func_abs'](-2)) print(items['mod_math'].sqrt(4)) 输出: {'number': 42, 'text': 'Hello World', 'func_abs': <built-in function abs>, 'mod_math': <module 'math' (built-in)>, 'error': <class 'ValueError'>, 'append': <built-in method append of list object at 0x00000234C1A21148>} 2 2.0
line ='gogo,100,20.2' #把字符串转成带有字段类型的列表 filed_type = [str,int,float] line = line.split(',') fileds = [ty(val) for ty,val in zip(filed_type,line)] print(fileds)
输出:
['gogo', 100, 20.2]
字符串的方法:
字典的方法: