为什么在定义类时括号是可选的,但在定义函数时是必需的?

时间:2022-06-28 01:35:56

In Python, defining a function with an empty parameter list requires a set of empty parenthesis. However, defining a class with the default superclass does not require a set of empty parenthesis; rather, those are optional, and appear to be uncommon. Why is it so?

在Python中,使用空参数列表定义函数需要一组空括号。但是,使用默认超类定义类不需要一组空括号;相反,这些是可选的,似乎并不常见。为什么会这样?

See also: Python class definition syntax.

另请参见:Python类定义语法。

1 个解决方案

#1


7  

I think the answer to your question is simply syntax. That is just the way Python is set up, but my take on how it got that way is:

我认为你的问题的答案只是语法。这就是Python的设置方式,但我对它如何实现的看法是:

I would think functions came out of mathematics things like:

我认为函数来自数学的东西,如:

f(x) = x

So when computer programming languages were being created there seems to have been some logical continuity from analog mathematics into programming languages.

因此,当创建计算机编程语言时,似乎已经存在从模拟数学到编程语言的一些逻辑连续性。

Classes on the other hand are more constructs of Computer Science, and repetitive memory management, so they were not created in such a fashion, but because they have a functional quality to them, they were given similar notation.

另一方面,类是计算机科学和重复内存管理的更多构造,因此它们不是以这种方式创建的,但是因为它们具有功能质量,所以它们被赋予类似的符号。

For Python, I will use the term method for function as that is the usual lingo...

对于Python,我将使用术语方法作为函数,因为这是通常的术语...

I understand your argument that both a class and method should be allowed to be defined using a short-cut in the no argument case:

我理解你的论点,即应该允许在无参数情况下使用快捷方式定义类和方法:

  • for classes when there is no inheritence
  • 对于没有继承的类
  • for methods when there are no arguments
  • 对于没有参数的方法

One reason I can think of is for consistency across usage and definition. Let's look at some examples:

我能想到的一个原因是使用和定义的一致性。我们来看一些例子:

definition:

定义:

def funcA():
    return 0

def funcB(arg):
    return arg

and you want to call that funciton:

你想打电话给那个功能:

>>> funcA()
>>> functB("an argument")

and

>>> f1 = funcA
>>> f2 = funcB
>>> f1()
>>> f2("another argument")

to pass references and call them.

传递引用并调用它们。

The syntax of the paranthesis between method declaration is consistent with calling the methods.

方法声明之间的paranthesis的语法与调用方法一致。

You need to put those empty parenthesis otherwise the interpreter will give you a reference to the method, and not actually call it.

你需要把那些空括号,否则解释器会给你一个方法的引用,而不是实际调用它。

So one benefit is it makes your code very clear.

所以一个好处是它使您的代码非常清晰。

definition:

定义:

class classA:
    pass

class classB(object):
    pass

usage:

用法:

# create an instance
my_instance_of_A = classA()
my_instance_of_B = classB()

# pass a reference
my_ref_to_A = classA
my_ref_to_B = classB

# call by reference
new_A = my_ref_to_A()
new_B = my_ref_to_B()

Here there is no change in behavior with regards to whether the class inherits or not, its calling behavior is dictated by what its internal or inherited __init__ method is defined as.

关于类是否继承,行为没有变化,其调用行为由其内部或继承的__init__方法定义为什么决定。

I think the current set up of requiring the empty () makes the code more readable to the untrained eye.

我认为当前需要empty()的设置使代码对未经训练的眼睛更具可读性。

If you really really really want to do what you ask, there is a workaround... you could always do this:

如果你真的真的想做你所要求的,那就有一个解决方法......你总是可以这样做:

func = lambda: "im a function declared with no arguments, and I didn't use parenthesis =p"

which can be called:

可以称之为:

>>> func
<function <lambda> at 0x6ffffef26e0>
>>> func()
"im a function declared with no arguments, and I didn't use parenthesis =p"

But the python holy book says No

但蟒蛇圣书说不

#1


7  

I think the answer to your question is simply syntax. That is just the way Python is set up, but my take on how it got that way is:

我认为你的问题的答案只是语法。这就是Python的设置方式,但我对它如何实现的看法是:

I would think functions came out of mathematics things like:

我认为函数来自数学的东西,如:

f(x) = x

So when computer programming languages were being created there seems to have been some logical continuity from analog mathematics into programming languages.

因此,当创建计算机编程语言时,似乎已经存在从模拟数学到编程语言的一些逻辑连续性。

Classes on the other hand are more constructs of Computer Science, and repetitive memory management, so they were not created in such a fashion, but because they have a functional quality to them, they were given similar notation.

另一方面,类是计算机科学和重复内存管理的更多构造,因此它们不是以这种方式创建的,但是因为它们具有功能质量,所以它们被赋予类似的符号。

For Python, I will use the term method for function as that is the usual lingo...

对于Python,我将使用术语方法作为函数,因为这是通常的术语...

I understand your argument that both a class and method should be allowed to be defined using a short-cut in the no argument case:

我理解你的论点,即应该允许在无参数情况下使用快捷方式定义类和方法:

  • for classes when there is no inheritence
  • 对于没有继承的类
  • for methods when there are no arguments
  • 对于没有参数的方法

One reason I can think of is for consistency across usage and definition. Let's look at some examples:

我能想到的一个原因是使用和定义的一致性。我们来看一些例子:

definition:

定义:

def funcA():
    return 0

def funcB(arg):
    return arg

and you want to call that funciton:

你想打电话给那个功能:

>>> funcA()
>>> functB("an argument")

and

>>> f1 = funcA
>>> f2 = funcB
>>> f1()
>>> f2("another argument")

to pass references and call them.

传递引用并调用它们。

The syntax of the paranthesis between method declaration is consistent with calling the methods.

方法声明之间的paranthesis的语法与调用方法一致。

You need to put those empty parenthesis otherwise the interpreter will give you a reference to the method, and not actually call it.

你需要把那些空括号,否则解释器会给你一个方法的引用,而不是实际调用它。

So one benefit is it makes your code very clear.

所以一个好处是它使您的代码非常清晰。

definition:

定义:

class classA:
    pass

class classB(object):
    pass

usage:

用法:

# create an instance
my_instance_of_A = classA()
my_instance_of_B = classB()

# pass a reference
my_ref_to_A = classA
my_ref_to_B = classB

# call by reference
new_A = my_ref_to_A()
new_B = my_ref_to_B()

Here there is no change in behavior with regards to whether the class inherits or not, its calling behavior is dictated by what its internal or inherited __init__ method is defined as.

关于类是否继承,行为没有变化,其调用行为由其内部或继承的__init__方法定义为什么决定。

I think the current set up of requiring the empty () makes the code more readable to the untrained eye.

我认为当前需要empty()的设置使代码对未经训练的眼睛更具可读性。

If you really really really want to do what you ask, there is a workaround... you could always do this:

如果你真的真的想做你所要求的,那就有一个解决方法......你总是可以这样做:

func = lambda: "im a function declared with no arguments, and I didn't use parenthesis =p"

which can be called:

可以称之为:

>>> func
<function <lambda> at 0x6ffffef26e0>
>>> func()
"im a function declared with no arguments, and I didn't use parenthesis =p"

But the python holy book says No

但蟒蛇圣书说不