Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事項
python 的語法定義和C++、matlab、java 還是很有區(qū)別的。
1. 括號與函數調用
def devided_3(x): return x/3.
print(a) #不帶括號調用的結果:<function a at 0x139c756a8>
print(a(3)) #帶括號調用的結果:1
不帶括號時,調用的是函數在內存在的首地址; 帶括號時,調用的是函數在內存區(qū)的代碼塊,輸入參數后執(zhí)行函數體。
2. 括號與類調用
class test():
y = 'this is out of __init__()'
def __init__(self):
self.y = 'this is in the __init__()'
x = test # x是類位置的首地址
print(x.y) # 輸出類的內容:this is out of __init__()
x = test() # 類的實例化
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
其實,這種情況最常用在卷積神經網絡中:
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
總結
以上所述是小編給大家介紹的Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Python按照某列內容對兩個DataFrame進行合并操作方法
這篇文章主要給大家介紹了關于Python按照某列內容對兩個DataFrame進行合并操作的相關資料,文中通過代碼示例介紹的非常詳細,對大家學習或者使用Python具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Python selenium根據class定位頁面元素的方法
這篇文章主要介紹了Python selenium根據class定位頁面元素的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
python?通過dict(zip)和{}的方式構造字典的方法
在python中,通常通過dict和zip組合來構建鍵值對,這篇文章主要介紹了python?通過dict(zip)和{}的方式構造字典的方法,需要的朋友可以參考下2022-07-07

