私が知らなかったPythonの機能が存在する
最近、私は新しい趣味を持っています-楽しみのためだけにPythonのドキュメントを読んでいます!暇なときに読むと、他の方法では見逃してしまう興味深い一口に気付く傾向があります。だから、これが私に言わせた「ピース」のリストです:
約!Pythonでそれを行うことができますか?
1.関数の属性
クラスやオブジェクトに属性を設定できるのと同じように、関数にも属性を設定できます。
def func(x):
intermediate_var = x**2 + x + 1
if intermediate_var % 2:
y = intermediate_var ** 3
else:
y = intermediate_var **3 + 1
# setting attributes here
func.optional_return = intermediate_var
func.is_awesome = 'Yes, my function is awesome.'
return y
y = func(3)
print('Final answer is', y)
# Accessing function attributes
print('Show calculations -->', func.optional_return)
print('Is my function awesome? -->', func.is_awesome)
«optional_return» 10 «is_awesome» 11. 19 20. :
Final answer is 2197
Show calculations --> 13
Is my function awesome? --> Yes, my function is awesome.
, , , return . , , .
2. for-else
Python else for. else , break.
my_list = ['some', 'list', 'containing', 'five', 'elements']
min_len = 3
for element in my_list:
if len(element) < min_len:
print(f'Caught an element shorter than {min_len} letters')
break
else:
print(f'All elements at least {min_len} letters long')
All elements at least 3 letters long
, else for, if. . , break. , else ( for) , .
, , break. , , , , . :
my_list = ['some', 'list', 'containing', 'five', 'elements']
min_len = 3
no_break = True
for element in my_list:
if len(element) < min_len:
print(f'Caught an element shorter than {min_len} letters')
no_break = False
break
if no_break:
print(f'All elements at least {min_len} letters long')
, .
3. int
10000000 100000000 ( ?). , Python, , , Python .
Python : . , 1_000_000 .
a = 3250
b = 67_543_423_778
print(type(a))
print(type(b))
print(type(a)==type(b))
<class 'int'>
<class 'int'>
True
4. eval () exec ()
Python Python . eval() exec() (‘eval’ ; ‘exec’ ).
a = 3
b = eval('a + 2')
print('b =', b)
exec('c = a ** 2')
print('c is', c)
b = 5
c is 9
eval() Python, b. 6 exec() Python .
. , 1000 _0, _1, .., _999 . , , .
, ( Python) eval/exec , , , , , . […] exec – Python, Python, , , – * *, exec .
’.
5. (Ellipsis)
( «…») - Python, None, True, False .. -, , (, , ):
5.1.
pass, , , .
def some_function():
...
def another_function():
pass
5.2. NONE
None , . None . .
# calculate nth odd number
def nth_odd(n):
if isinstance(n, int):
return 2 * n - 1
else:
return None
# calculate the original n of nth odd number
def original_num(m=...):
if m is ...:
print('This function needs some input')
elif m is None:
print('Non integer input provided to nth_odd() function')
elif isinstance(m, int):
if m % 2:
print(f'{m} is {int((m + 1)/2)}th odd number')
else:
print(f'{m} is not an odd number')
original_num()
a = nth_odd(n='some string')
original_num(a)
b = nth_odd(5)
original_num(b)
original_num(16)
nth_odd() n- , c n. original_num() n, n- . None – original_num(), m. :
This function needs some input
Non integer input provided to nth_odd() function
9 is 5th odd number
16 is not an odd number
5.3. NumPy
NumPy . :
import numpy as np
a = np.arange(16).reshape(2,2,2,2)
print(a[..., 0].flatten())
print(a[:, :, :, 0].flatten())
[ 0 2 4 6 8 10 12 14]
[ 0 2 4 6 8 10 12 14]
, ‘…’ , ‘:’, .
None(ブール値がFalse)とは異なり、省略記号のブール値はTrueです。
TL; DR
そこで、次のような興味深い機能を発見しました。
関数属性:オブジェクトだけでなく関数にも属性を割り当てます。
For-else Loop:ループがbreakステートメントなしで実行されたかどうかを追跡します。
intの区切り文字:32_534_478はintです。
eval()およびexec():行をPythonコードとして読み取り、実行します。
省略記号:一般的な組み込み定数。
別れの言葉
Pythonは便利な言語であるだけでなく、非常に興味深い言語です。私たちは皆、私たちの生活で忙しいですが、これはそれ自体のために言語を学ぶことを妨げるものではありません。私はあなたが見つけるかもしれないイースターエッグについてもっと知りたいです。