Three minutes, let you use parentheses to clarify the functions in Python

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meiguanxi7878/article/details/102724932

Has been using python function in parentheses, could not tell a bit, in the end when to use parentheses, brackets when not to, caused great confusion.

Results of the:

According to the results to analyze:

1, x = aaa aaa is a class name, not behind brackets, print the results, it indicates that x is a class

2, the absence of brackets, not instantiating the class, the result y is public property, so as xy print you

. 3, as a function foo aaa class, method or property referred to, but has not foo brackets behind, the print result as an address in memory foo

Notice that the print (x.foo ()), or print (x.foo ( 'aa')) are given,

Thus, according to the prompts, foo should be followed with two parameters, here why it should take two parameters. Because there is no initialization x, x is not passed as a parameter in the function foo

and so

As can be seen, herein it refers to a class itself self

4, x = aaa () class parentheses behind aaa, aaa print result class memory address

5, since x = aaa () indicates aaa is instantiated, therefore call the constructor of y, the printed result is the value self.y

6 shows a method foo class, foo without parentheses, the function is not executed.

And 7. 8, foo parentheses, perform the function foo. Here foo with only one parameter in the "u" or "cc", and not with two parameters mentioned above, since aaa instantiated

So he concludes.

对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

类带括号,类实例化,

函数带括号,执行函数,返回函数的结果

否则不执行,只是调用类或函数本身。

觉得文章还可以的话不妨收藏起来慢慢看,有任何意见或者看法欢迎大家评论!

Guess you like

Origin blog.csdn.net/meiguanxi7878/article/details/102724932