经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
来源:jb51  时间:2018/12/3 9:48:06  对本文有异议

python 的语法定义和C++、matlab、java 还是很有区别的。

1. 括号与函数调用

  1. def devided_3(x):
  2. return x/3.

print(a)    #不带括号调用的结果:<function a at 0x139c756a8>
print(a(3)) #带括号调用的结果:1

不带括号时,调用的是函数在内存在的首地址; 带括号时,调用的是函数在内存区的代码块,输入参数后执行函数体。

2. 括号与类调用

  1. class test():
  2. y = 'this is out of __init__()'
  3. def __init__(self):
  4. self.y = 'this is in the __init__()'
  5. x = test # x是类位置的首地址
  6. print(x.y) # 输出类的内容:this is out of __init__()
  7. x = test() # 类的实例化
  8. print(x.y) # 输出类的属性:this is in the __init__() ;

3. function(#) (input)

  1. def With_func_rtn(a):
  2. print("this is func with another func as return")
  3. print(a)
  4. def func(b):
  5. print("this is another function")
  6. print(b)
  7. return func
  8. func(2018)(11)
  9. >>> this is func with another func as return
  10. 2018
  11. this is another function
  12. 11

其实,这种情况最常用在卷积神经网络中:

  1. def model(input_shape):
  2. # Define the input placeholder as a tensor with shape input_shape.
  3. X_input = Input(input_shape)
  4. # Zero-Padding: pads the border of X_input with zeroes
  5. X = ZeroPadding2D((3, 3))(X_input)
  6. # CONV -> BN -> RELU Block applied to X
  7. X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
  8. X = BatchNormalization(axis = 3, name = 'bn0')(X)
  9. X = Activation('relu')(X)
  10. # MAXPOOL
  11. X = MaxPooling2D((2, 2), name='max_pool')(X)
  12. # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
  13. X = Flatten()(X)
  14. X = Dense(1, activation='sigmoid', name='fc')(X)
  15. # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
  16. model = Model(inputs = X_input, outputs = X, name='HappyModel')
  17. return model

总结

以上所述是小编给大家介绍的Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号