當前位置:首頁 » 編程語言 » pythoninscope

pythoninscope

發布時間: 2023-01-09 02:00:05

㈠ 如何在python中獲取完整的異顏桓

我們可以很容易的通過Python解釋器獲取幫助。如果想知道一個對象(object)更多的信息,那麼可以調用help(object)!另外還有一些有用的方法,dir(object)會顯示該對象的大部分相關屬性名,還有object._doc_會顯示其相對應的文檔字元串。下面對其進行逐一介紹。

1、 help()

help函數是Python的一個內置函數。
函數原型:help([object])。
可以幫助我們了解該對象的更多信息。
Ifno argument is given, the interactive help system starts on the interpreter console.

>>> help()

Welcome to Python 2.7! This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at .

Enter the name of any mole, keyword, or topic to get help on writing
Python programs and using Python moles. To quit this help utility andreturn to the interpreter, just type "quit".

To get a list of available moles, keywords, or topics, type "moles","keywords", or "topics". Each mole also comes with a one-line summary
of what it does; to list the moles whose summaries contain a given word
such as "spam", type "moles spam".

help> int # 由於篇幅問題,此處只顯示部分內容,下同Help on class int in mole __builtin__:class int(object)
| int(x=0) -> int or long
| int(x, base=10) -> int or long
|

.....help>

Ifthe argument is a string, then the string is looked up as the name of amole,function,class,method,keyword, ordocumentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.

>>> help(abs) # 查看abs函數Help on built-in function abs in mole __builtin__:

abs(...)
abs(number) -> number

Return the absolute value of the argument.>>> help(math) # 查看math模塊,此處只顯示部分內容Help on built-in mole math:

NAME
math

FILE
(built-in)

DESCRIPTION
This mole is always available. It provides access to the
mathematical functions defined by the C standard.

FUNCTIONS
acos(...)
acos(x)

Return the arc cosine (measured in radians) of x.

.....>>> 293031

2、dir()

dir函數是Python的一個內置函數。
函數原型:dir([object])
可以幫助我們獲取該對象的大部分相關屬性。
Without arguments, return the list of names in the current local scope.

>>> dir() # 沒有參數['__builtins__', '__doc__', '__name__', '__package__']>>> >>> import math # 引入一個包和一個變數,再次dir()>>> a=3>>> >>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'math']>>> 12345678910

With an argument, attempt to return a list of valid attributes for that object.

>>> import math>>> dir(math) # math模塊作為參數['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'sign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']>>> 12345

The default dir() mechanism behaves differently with different types of objects, as it attempts to proce the most relevant, rather than complete, information:
• If the object is a mole object, the list contains the names of the mole』s attributes.

>>> import math>>> dir(math) # math模塊作為參數['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'sign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']>>> 12345

• 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.

>>> dir(float) # 類型['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__rece__', '__rece_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']>>> dir(3.4)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__rece__', '__rece_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']>>> >>> class A:
x=3
y=4>>> class B(A):
z=5>>> dir(B) # 類['__doc__', '__mole__', 'x', 'y', 'z']>>> 123456789101112131415161718

• 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.

3、_doc_

在Python中有一個奇妙的特性,文檔字元串,又稱為DocStrings。
用它可以為我們的模塊、類、函數等添加說明性的文字,使程序易讀易懂,更重要的是可以通過Python自帶的標准方法將這些描述性文字信息輸出。
上面提到的自帶的標准方法就是_doc_。前後各兩個下劃線。
註:當不是函數、方法、模塊等調用doc時,而是具體對象調用時,會顯示此對象從屬的類型的構造函數的文檔字元串。

>>> import math>>> math.__doc__ # 模塊'This mole is always available. It provides access to the mathematical functions defined by the C standard.'>>> abs.__doc__ # 內置函數'abs(number) -> number Return the absolute value of the argument.'>>> def addxy(x,y):
'''the sum of x and y'''
return x+y>>> addxy.__doc__ # 自定義函數'the sum of x and y'>>> a=[1,2,4]>>> a.count.__doc__ # 方法'L.count(value) -> integer -- return number of occurrences of value'>>> b=3>>> b.__doc__ # 具體的對象"int(x=0) -> int or long int(x, base=10) -> int or long Convert a number or string to an integer, or return 0 if no arguments are given. If x is floating point, the conversion truncates towards zero. If x is outside the integer range, the function returns a long instead. If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4">>> 12345678910111213141516171819

其實我們可以通過一定的手段來查看這些文檔字元串,比如使用Pycharm,在對應的模塊、函數、方法等上滑鼠「右擊」->Go to->Declaration。例如:查看內置函數abs的文檔字元串

參考文獻:
1、Python幫助文檔

㈡ python中如何在函數中把字元串中的global語句執行

[root@-xlPythonTest]#vimstu.py
#!/usr/bin/python

#coding=utf-8

scope={}

defaddstu():

code=raw_input('請輸入學生的學號')

exec('d'+code+'='+code)inscope

printscope['d'+code]

addstu()
[root@-xlPythonTest]#pythonstu.py

請輸入學生的學號123

123

法二:傳參數也行

#!/usr/bin/python
#coding=utf-8
defaddstu(scope):
code=raw_input('請輸入學生的學號:')
scope['d'+code]=code
printscope['d'+code]
scope={}
addstu(scope)
printscope
[root@-xlPythonTest]#pythonstu.py

請輸入學生的學號:456

456

{'d456':'456'}

㈢ python中 *= 是什麼意思

1、兩個值相加,然後返回值給符號左側的變數

舉例如下:

>>> a=1

>>> b=3

>>> a+=b(或者a+=3)

>>> a

4

2、用於字元串連接(變數值帶引號,數據類型為字元串)

>>> a='1'

>>> b='2'

>>> a+=b

>>> a

'12'

8、運算符優先順序

以下所列優先順序順序按照從低到高優先順序的順序;同行為相同優先順序。

Lambda #運算優先順序最低

邏輯運算符: or

邏輯運算符: and

邏輯運算符:not

成員測試: in, not in

同一性測試: is, is not

比較: <,<=,>,>=,!=,==

按位或: |

按位異或: ^

按位與: &

移位: << ,>>

加法與減法: + ,-

乘法、除法與取余: *, / ,%

正負號: +x,-x

具有相同優先順序的運算符將從左至右的方式依次進行,用小括弧()可以改變運算順序。

參考資料來源:網路-Python

㈣ python基礎教程 10-11例子如何執行

2020年最新Python零基礎教程(高清視頻)網路網盤

鏈接:

提取碼: 5kid 復制這段內容後打開網路網盤手機App,操作更方便哦

若資源有問題歡迎追問~


㈤ python中的for in的相關問題

A.因為已經引入了sqrt函數,如果在自己的空間中執行 sqrt=1 '把sqrt當成了一個變數,再運行sqrt(4)會出錯
例子中 exec運行代碼 在Scope空間中 sqrt是個變數,賦值1,如果沒有in scope,那麼exce運行空間就是本空間,再次sqrt(4)就會出錯。

對比:
a=1234
exec 'a=4321'
a
4321
和代碼:
a=1234
b={}
exec 'a=4321' in b
a
1234

B:print [x*x for x in range(10) if x % 3 == 0]

這里range(10)產生0,1,2~8,9 這10個數字
後面添加了條件x % 3 ==0,就是判斷 x除以3的余數 等於0, 篩選出0、3、6、9
傳遞給x*x,就產生了一個列表:[0,9,36,81]

㈥ python編程問題

錯誤:

復制代碼代碼如下:

>>> def f(x, y):
print x, y
>>> t = ('a', 'b')
>>> f(t)

Traceback (most recent call last):
File "<pyshell#65>", line 1, in <mole>
f(t)
TypeError: f() takes exactly 2 arguments (1 given)

【錯誤分析】不要誤以為元祖里有兩個參數,將元祖傳進去就可以了,實際上元祖作為一個整體只是一個參數,
實際需要兩個參數,所以報錯。必需再傳一個參數方可.

復制代碼代碼如下:

>>> f(t, 'var2')
('a', 'b') var2

更常用的用法: 在前面加*,代表引用元祖

復制代碼代碼如下:

>>> f(*t)
'a', 'b'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
錯誤:

復制代碼代碼如下:

>>> def func(y=2, x):
return x + y
SyntaxError: non-default argument follows default argument

【錯誤分析】在C++,Python中默認參數從左往右防止,而不是相反。這可能跟參數進棧順序有關。

復制代碼代碼如下:

>>> def func(x, y=2):
return x + y
>>> func(1)
3

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

錯誤:

復制代碼代碼如下:

>>> D1 = {'x':1, 'y':2}
>>> D1['x']
1
>>> D1['z']

Traceback (most recent call last):
File "<pyshell#185>", line 1, in <mole>
D1['z']
KeyError: 'z'

【錯誤分析】這是Python中字典鍵錯誤的提示,如果想讓程序繼續運行,可以用字典中的get方法,如果鍵存在,則獲取該鍵對應的值,不存在的,返回None,也可列印提示信息.

復制代碼代碼如下:

>>> D1.get('z', 'Key Not Exist!')
'Key Not Exist!'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

錯誤:

復制代碼代碼如下:

>>> from math import sqrt
>>> exec "sqrt = 1"
>>> sqrt(4)

Traceback (most recent call last):
File "<pyshell#22>", line 1, in <mole>
sqrt(4)
TypeError: 'int' object is not callable

【錯誤分析】exec語句最有用的地方在於動態地創建代碼字元串,但裡面存在的潛在的風險,它會執行其他地方的字元串,在CGI中更是如此!比如例子中的sqrt = 1,從而改變了當前的命名空間,從math模塊中導入的sqrt不再和函數名綁定而是成為了一個整數。要避免這種情況,可以通過增加in <scope>,其中<scope>就是起到放置代碼字元串命名空間的字典。

復制代碼代碼如下:

>>> from math import sqrt
>>> scope = {}
>>> exec "sqrt = 1" in scope
>>> sqrt(4)
2.0

㈦ python函數

參數match是正則表達式匹配後的結果,match.group(1)就是返回結果1。

importre
m=re.search('(^.+?) (.+?$)','print"111" print"222"')
printm.group(1)#print"111"

eval()一般是用來執行字元串代碼,也就是命令注入。

其中的參數code:就是要執行的代碼,比如print "111"
其中的參數scope:是code執行范圍的字典.

由於匹配的字元串代碼經常有格式對齊等問題,所以加一個try except來捕捉。

exec跟eval類似,可以執行代碼,但是只是一個語法,沒有返回值。

exec code in scope就是執行code作用范圍為scope字典

㈧ Python3 exec函數 scope失效 exec('sqrt = 1' in scope)

是exec('sqrt = 1' )in scope
python3下應該是:
exec('sqrt = 1' , scope)

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:677
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:250
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:799
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705