Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Python中调用类内的函数_Python - Fatal编程技术网

无法在Python中调用类内的函数

无法在Python中调用类内的函数,python,Python,我正在学习Python。当我试图调用类中的函数时,我遇到了这个错误。所以请帮我解决这个问题 tony@kali:~$ python Python 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class H: ...

我正在学习Python。当我试图调用类中的函数时,我遇到了这个错误。所以请帮我解决这个问题

tony@kali:~$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08) 
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class H:
...            def y():
...                  print "j"
... 
>>> g=H
>>> g.y()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method y() must be called with H instance as first argument (got nothing instead)
>>> H.y()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method y() must be called with H instance as first argument (got nothing instead)
>>> 
tony@kali:~$python
Python 2.7.13(默认值,2017年1月19日,14:48:08)
[GCC 6.3.0 20170118]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>H类:
...            定义y():
...                  打印“j”
... 
>>>g=H
>>>g.y()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:必须使用H实例作为第一个参数调用未绑定的方法y()(而不是获取任何内容)
>>>H.y()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:必须使用H实例作为第一个参数调用未绑定的方法y()(而不是获取任何内容)
>>> 

您必须初始化
类,然后只有您可以调用该
类的方法。另外,方法的第一个参数应该是
self

class H:
   def y(self):
         print "j"
g = H()
g.y()
有关self的更多详细信息,请参阅


你一定要检查课程。

你有答案和评论,所以让我解释一下错误信息

在您的代码中,
g
H
是同一事物的不同名称,因此这两个错误是一个

现在,
H
是一个(旧式)类,它下面的所有函数
def
,除非用
classmethod
staticmethod
等技巧“处理”,否则都是方法。当您作为类
H
的属性访问这些方法时,它们被称为“未绑定”方法,因为它们尚未绑定到类
H
的实例

为了理解Python的类、方法和属性的核心模型是如何实现的,我推荐Shalabh Chaturvedi的优秀文章:

tl;dr版本:通常,您需要通过调用实例的绑定方法来调用方法:

Python将把该实例(
h_instance
放在伪代码段中)作为该方法的第一个参数。如果将其称为
h_instance.y()
,则实际参数列表为
(h_instance)
;如果将其称为
h\u instance.y(a)
,则参数为
(h\u instance,a)

这就是为什么您几乎总是发现方法定义中的第一个形式参数是
self
。这个名称没有什么特别之处,只是Python希望为调用该方法的实例保留该插槽。它允许您使用空参数列表定义方法,但当您实际使用该实例时,该参数列表将不会有用。在这种情况下,您将始终得到
TypeError
——因为调用发送一个参数(“code>self
”实例),而方法定义中没有参数

作为演示,考虑下面的会话:

In [1]: class H(object):    # New-style class, doesn't matter here.
   ...:     def y(self):    # Notice the call signature: "self" included
   ...:         print "j"        

In [2]: h_instance = H()    # Create an instance from the class

# This is how normally you're expected to call. There's no argument
# passed to the *bound* to the instance h_instance, because when you do
# this Python will carry out the syntactic desugaring and call on the
# instance.
In [3]: h_instance.y()
j

# This does the same thing and demonstrates the concept of an
# unbound method.  It's not bound to any specific instance of H, so
# you'll have to specify which instance to call.
In [4]: H.y(h_instance)
j

您应该使用
g=H()
获取类的实例。您创建了一个方法。为什么需要将其作为函数调用?您不必在Python中使用类,只需不将其放在方法中即可。否则,请使用
staticmethod
decorator,但是您有一个没有状态的类。@Loolou:这只是问题的一部分,没有
self
参数。@MartijnPieters是的,您的参考一般性评论是,您的帖子将从一些格式化和清理中受益。这将是邀请和很好的人谁捐赠他们的时间免费给你。但这样的过程也有助于让你自己更清楚地了解事情
In [1]: class H(object):    # New-style class, doesn't matter here.
   ...:     def y(self):    # Notice the call signature: "self" included
   ...:         print "j"        

In [2]: h_instance = H()    # Create an instance from the class

# This is how normally you're expected to call. There's no argument
# passed to the *bound* to the instance h_instance, because when you do
# this Python will carry out the syntactic desugaring and call on the
# instance.
In [3]: h_instance.y()
j

# This does the same thing and demonstrates the concept of an
# unbound method.  It's not bound to any specific instance of H, so
# you'll have to specify which instance to call.
In [4]: H.y(h_instance)
j