日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python表示当前对象_对象操作

發(fā)布時間:2025/3/15 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:返回對象的類型,或者根據傳入的參數創(chuàng)建一個新的類型

~~~

>>> type(1) # 返回對象的類型

#使用type函數創(chuàng)建類型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表示当前对象_对象操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。