python表示当前对象_对象操作
[TOC]
# 對象操作
## help:返回對象的幫助信息
~~~
>>> help(str)
Help on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
***************************
~~~
## dir:返回對象或者當前作用域內的屬性列表
~~~
>>> import math
>>> math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
~~~
## id:返回對象的唯一標識符
~~~
>>> a = 'some text'
>>> id(a)
69228568
~~~
## hash:獲取對象的哈希值
~~~
>>> hash('good good study')
1032709256
~~~
## type:返回對象的類型,或者根據傳入的參數創建一個新的類型
~~~
>>> type(1) # 返回對象的類型
#使用type函數創建類型D,含有屬性InfoD
>>> D = type('D',(A,B),dict(InfoD='some thing defined in D'))
>>> d = D()
>>> d.InfoD
'some thing defined in D'
~~~
## ascii:返回對象的可打印表字符串表現方式
~~~
>>> ascii(1)
'1'
>>> ascii('&')
"'&'"
>>> ascii(9000000)
'9000000'
>>> ascii('中文') #非ascii字符
"'\\u4e2d\\u6587'"
~~~
## format:格式化顯示值
~~~
#字符串可以提供的參數 's' None
>>> format('some string','s')
'some string'
>>> format('some string')
'some string'
#整形數值可以提供的參數有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None
>>> format(3,'b') #轉換成二進制
'11'
>>> format(97,'c') #轉換unicode成字符
'a'
>>> format(11,'d') #轉換成10進制
'11'
>>> format(11,'o') #轉換成8進制
'13'
>>> format(11,'x') #轉換成16進制 小寫字母表示
'b'
>>> format(11,'X') #轉換成16進制 大寫字母表示
'B'
>>> format(11,'n') #和d一樣
'11'
>>> format(11) #默認和d一樣
'11'
#浮點數可以提供的參數有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None
>>> format(314159267,'e') #科學計數法,默認保留6位小數
'3.141593e+08'
>>> format(314159267,'0.2e') #科學計數法,指定保留2位小數
'3.14e+08'
>>> format(314159267,'0.2E') #科學計數法,指定保留2位小數,采用大寫E表示
'3.14E+08'
>>> format(314159267,'f') #小數點計數法,默認保留6位小數
'314159267.000000'
>>> format(3.14159267000,'f') #小數點計數法,默認保留6位小數
'3.141593'
>>> format(3.14159267000,'0.8f') #小數點計數法,指定保留8位小數
'3.14159267'
>>> format(3.14159267000,'0.10f') #小數點計數法,指定保留10位小數
'3.1415926700'
>>> format(3.14e+1000000,'F') #小數點計數法,無窮大轉換成大小字母
'INF'
#g的格式化比較特殊,假設p為格式中指定的保留小數位數,先嘗試采用科學計數法格式化,得到冪指數exp,如果-4<=exp
>>> format(0.00003141566,'.1g') #p=1,exp=-5 ==》 -4<=exp
'3e-05'
>>> format(0.00003141566,'.2g') #p=1,exp=-5 ==》 -4<=exp
'3.1e-05'
>>> format(0.00003141566,'.3g') #p=1,exp=-5 ==》 -4<=exp
'3.14e-05'
>>> format(0.00003141566,'.3G') #p=1,exp=-5 ==》 -4<=exp
'3.14E-05'
>>> format(3.1415926777,'.1g') #p=1,exp=0 ==》 -4<=exp
'3'
>>> format(3.1415926777,'.2g') #p=1,exp=0 ==》 -4<=exp
'3.1'
>>> format(3.1415926777,'.3g') #p=1,exp=0 ==》 -4<=exp
'3.14'
>>> format(0.00003141566,'.1n') #和g相同
'3e-05'
>>> format(0.00003141566,'.3n') #和g相同
'3.14e-05'
>>> format(0.00003141566) #和g相同
'3.141566e-05'
~~~
## vars:返回當前作用域內的局部變量和其值組成的字典,或者返回對象的屬性列表
~~~
#作用于類實例
>>> class A(object):
pass
>>> a.__dict__
{}
>>> vars(a)
{}
>>> a.name = 'Kim'
>>> a.__dict__
{'name': 'Kim'}
>>> vars(a)
{'name': 'Kim'}
~~~
總結
以上是生活随笔為你收集整理的python表示当前对象_对象操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动驾驶芯片_自动驾驶芯片“争夺战”
- 下一篇: new 结构体指针_Go:我应该用指针替