Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事項(xiàng)
python 的語法定義和C++、matlab、java 還是很有區(qū)別的。
1. 括號與函數(shù)調(diào)用
def devided_3(x): return x/3.
print(a) #不帶括號調(diào)用的結(jié)果:<function a at 0x139c756a8>
print(a(3)) #帶括號調(diào)用的結(jié)果:1
不帶括號時(shí),調(diào)用的是函數(shù)在內(nèi)存在的首地址; 帶括號時(shí),調(diào)用的是函數(shù)在內(nèi)存區(qū)的代碼塊,輸入?yún)?shù)后執(zhí)行函數(shù)體。
2. 括號與類調(diào)用
class test():
y = 'this is out of __init__()'
def __init__(self):
self.y = 'this is in the __init__()'
x = test # x是類位置的首地址
print(x.y) # 輸出類的內(nèi)容:this is out of __init__()
x = test() # 類的實(shí)例化
print(x.y) # 輸出類的屬性:this is in the __init__() ;
3. function(#) (input)
def With_func_rtn(a):
print("this is func with another func as return")
print(a)
def func(b):
print("this is another function")
print(b)
return func
func(2018)(11)
>>> this is func with another func as return
2018
this is another function
11
其實(shí),這種情況最常用在卷積神經(jīng)網(wǎng)絡(luò)中:
def model(input_shape):
# Define the input placeholder as a tensor with shape input_shape.
X_input = Input(input_shape)
# Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input)
# CONV -> BN -> RELU Block applied to X
X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
X = BatchNormalization(axis = 3, name = 'bn0')(X)
X = Activation('relu')(X)
# MAXPOOL
X = MaxPooling2D((2, 2), name='max_pool')(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs = X_input, outputs = X, name='HappyModel')
return model
總結(jié)
以上所述是小編給大家介紹的Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
python函數(shù)式編程學(xué)習(xí)之yield表達(dá)式形式詳解
這篇文章主要給大家介紹了關(guān)于python函數(shù)式編程學(xué)習(xí)之yield表達(dá)式形式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2018-03-03
Python編程中的for循環(huán)語句學(xué)習(xí)教程
這篇文章主要介紹了Python編程中的for循環(huán)語句學(xué)習(xí)教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-10-10
Python callable()函數(shù)用法實(shí)例分析
這篇文章主要介紹了Python callable()函數(shù)用法,結(jié)合實(shí)例形式分析了Python callable()函數(shù)的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-03-03
pandas中關(guān)于apply+lambda的應(yīng)用
本文主要介紹了pandas中關(guān)于apply+lambda的應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

