在构造函数中调用函数时出现 NameError

发布于 2024-11-29 01:53:08 字数 935 浏览 0 评论 0原文

我通过首先调用构造函数中的函数来运行下面的代码

-

>>> class PrintName:
...    def __init__(self, value):
...      self._value = value
...      printName(self._value)
...    def printName(self, value):
...      for c in value:
...        print c
...
>>> o = PrintName('Chaitanya')
C
h
a
i
t
a
n
y
a

我再次运行它并得到这个

>>> class PrintName:
...    def __init__(self, value):
...      self._value = value
...      printName(self._value)
...    def printName(self, value):
...      for c in value:
...        print c
...
>>> o = PrintName('Hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
NameError: global name 'printName' is not defined

Can I not call a function in the constructor?类似代码的执行为何会出现偏差?

注意:我忘记使用 self(例如:self.printName())调用类的本地函数。为该帖子道歉。

I ran the code below, by calling the function in the constructor

First --

>>> class PrintName:
...    def __init__(self, value):
...      self._value = value
...      printName(self._value)
...    def printName(self, value):
...      for c in value:
...        print c
...
>>> o = PrintName('Chaitanya')
C
h
a
i
t
a
n
y
a

Once again I run this and I get this

>>> class PrintName:
...    def __init__(self, value):
...      self._value = value
...      printName(self._value)
...    def printName(self, value):
...      for c in value:
...        print c
...
>>> o = PrintName('Hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
NameError: global name 'printName' is not defined

Can I not call a function in the constructor? and whay a deviation in the execution of similar code?

Note: I forgot to call a function local to the class, by using self (ex: self.printName()). Apologize for the post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我闹别瞎闹 2024-12-06 01:53:08

您需要调用 self.printName ,因为您的函数是属于 PrintName 类的方法。

或者,由于您的 printname 函数不需要依赖于对象状态,因此您可以将其设为模块级函数。

class PrintName:
    def __init__(self, value):
        self._value = value
        printName(self._value)

def printName(value):
    for c in value:
    print c

You need to call self.printName since your function is a method belonging to the PrintName class.

Or, since your printname function doesn't need to rely on object state, you could just make it a module level function.

class PrintName:
    def __init__(self, value):
        self._value = value
        printName(self._value)

def printName(value):
    for c in value:
    print c
柒七 2024-12-06 01:53:08

而不是

printName(self._value)

您想要的,因为您在父作用域中有另一个函数 printName

self.printName(self._value)

它可能第一次起作用,

Instead of

printName(self._value)

you wanted

self.printName(self._value)

It probably worked the first time because you had another function printName in a parent scope.

回忆追雨的时光 2024-12-06 01:53:08

您想要的是 __init__ 中的 self.printName(self._value),而不仅仅是 printName(self._value)

What you want is self.printName(self._value) in __init__, not just printName(self._value).

带上头具痛哭 2024-12-06 01:53:08

我知道这是一个老问题,但我只是想补充一点,您也可以使用类名并传递 self 作为第一个参数来调用该函数。

不知道你为什么想要这样做,因为我认为这可能会让事情变得不那么清楚。

class PrintName:
    def __init__(self, value):
        self._value = value
        PrintName.printName(self, self._value)

    def printName(self, value):
        for c in value:
        print(c)

有关详细信息,请参阅 python 手册的第 9 章:

9.3.4。方法对象

实际上,您可能已经猜到了答案:方法的特殊之处在于对象作为函数的第一个参数传递。在我们的示例中,调用 xf() 与 MyClass.f(x) 完全相同。一般来说,调用带有 n 个参数列表的方法相当于使用参数列表调用相应的函数,该参数列表是通过在第一个参数之前插入方法的对象而创建的。

I know this is an old question, but I just wanted to add that you can also call the function using the Class name and passing self as the first argument.

Not sure why you'd want to though, as I think it might make things less clear.

class PrintName:
    def __init__(self, value):
        self._value = value
        PrintName.printName(self, self._value)

    def printName(self, value):
        for c in value:
        print(c)

See Chapter 9 of the python manuals for more info:

9.3.4. Method Objects

Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文