当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项

Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项

2018年12月04日  | 移动技术网IT编程  | 我要评论

48775,萧如瑟结婚,qq空间关闭网址

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

1. 括号与函数调用

def devided_3(x):
   return x/3.

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

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

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.*中的注意,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网