转自:https://www.cnblogs.com/ywhyme/p/6913057.html
在Python中遇见了带不带括号的问题,我目前的是这三种,有问题请指出.如果有其他的,我后续会更新
一 函数带不带括号:
1
2
3
4
5
|
def
a(x):
return
x
print
(a)
#不带括号调用的结果:<function a at 0x1091766a8>
print
(a(
3
))
#带括号调用的结果:3
|
1.不带括号时,调用的是这个函数本身
2、带括号(此时必须传入需要的参数),调用的是函数的return结果
摘自: http://blog.csdn.net/foryouslgme/article/details/51483867
二. class实例化带不带括号:
1 class clseest(): 2 y = '我 ' 3 def __init__(self): 4 self.y = '你' 5 x = clseest 6 print(x.y) #我 7 8 x = clseest() 9 print(x.y)#你
即:带括号的实例化方法是调用了__init__()函数, 此时必须传入需要的参数
摘自: http://bbs.csdn.net/topics/392156189
三 class新建的时候要不要加
class MyMsg:
是旧的书写方式
class MyMsg(object):
是新的书写方式,所以建议使用新的书写方式
Ps:即没有差别,不过要是有继承必须写():建议写()