【问题标题】:NameError: global name 'MyClass' is not defined in Pepper/NaoNameError:全局名称“MyClass”未在 Pepper/Nao 中定义
【发布时间】:2018-11-03 12:40:34
【问题描述】:

更新:为了解决 Choregraphe 和 Python 的结合,我拒绝了使用 @classmethod 的想法。相反,当我想使用 MyClass 时,我会在 MyCustomClass 中引发 AlMemory 事件。

我阅读了很多关于 NameError 的帖子,但仍然找不到解决问题的方法。

我使用 Python 框为 Nao 编写了一个带有 Choregraphe 的程序。

我得到了以下内容:

class MyClass(GeneratedClass): #GeneratedClass is given

 def __init__(self):
    GeneratedClass.__init__(self)

 @classmethod
 def doSomething(cls, a):
    print a

class myCustomClass():
 def func(self):
    MyClass.doSomething(a)

当从 myCustomClass 调用 func() 时,我在 MyClass 上得到了 NameError

[ERROR] behavior.box :FMBox::createPythonModule:0 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1275012824__root__test_1:用户类评估失败并出现以下错误: 未定义全局名称“MyClass”

我该如何解决这个问题?

【问题讨论】:

  • 您应该将代码更新为标准的 4 个空格缩进。
  • doSomething 方法定义在哪里?
  • do 也是 Python 中的关键字,请为此方法使用其他名称。
  • @Hitobat 不,不是。
  • 最新状态:根据@Rarblack 的反馈,我将self 更改为cls。但是还是不行。

标签: python-2.7 class nao-robot pepper choregraphe


【解决方案1】:

首先,您的 @method类结构 错误

当我运行你的代码时,它说:

class MyClass(GeneratedClass):

 @classmethod
 def do(self, a):
     return a

class myCustomClass():
 def func(self):
    MyClass.do(a)

输出:

Traceback (most recent call last):
  File "test.py", line 236, in <module>
    class MyClass(GeneratedClass):
NameError: name 'GeneratedClass' is not defined

你的班级结构完全错误。如果要传递参数,请使用__init__ 方法。

class MyClass:
    def __init__(self, GeneratedClass):
        self.generated_class = GeneratedClass

     def do(self):
         doSomething(self.generated_class)

class MyCustomClass:
    def func(self):
        GeneratedClass = 1
        MyClass(GeneratedClass).do()

myCustomClass().func()

如果您使用@methodclass,则不应传递self,它是cls。就像在这个例子中一样:

from datetime import date

# random Person
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear)

    def display(self):
        print(self.name + "'s age is: " + str(self.age))

person = Person('Adam', 19)
person.display()

person1 = Person.fromBirthYear('John',  1985)
person1.display()

如果您正在尝试继承,请举个例子。

class Mapping:
    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)

    __update = update   # private copy of original update() method

class MappingSubclass(Mapping):

    def update(self, keys, values):
        # provides new signature for update()
        # but does not break __init__()
        for item in zip(keys, values):
            self.items_list.append(item)

现在全部根据您的要求合二为一:

class GeneratedClass:
    def __init__(self):
        self.myclass = self

    def print(self):
        print('hello_people')

class MyClass(GeneratedClass):

    def __init__(self,a):
        self.a = a
        GeneratedClass.__init__(self)
        print(a)

    @classmethod
    def give_param(cls, a):
        return cls(a)

class myCustomClass:
    def func(self):
        MyClass.give_param('aa')

myCustomClass().func()

注意:我使用过 python 3.x

【讨论】:

  • 首先,类GeneratedClass是存在的。我无权访问此类的代码。 MyClass 初始化为 def __init__(self): GeneratedClass.__init__(self)。但即使我将self 更改为cls。它对我不起作用。你的例子也不是:(我不确定这是否是Choregraphe的特色。
  • @catie 如果您尝试使用 classmethod,则无法传递 self,首先是 cls,其次您的代码不起作用的原因是您尝试使用 classmethod 但不返回任何类. MyClass.dosomething(a) 是调用 classmethod 的方式。正如我在回答中提到的,classmethod 应该返回 cls() 因为你的 init 不需要参数。
  • 另外,如果您仔细阅读我的回答,您将看到问题的直接解决方案。由于缺少库等,我无法自己运行您的代码来检查它。
  • 一个简单的解决方案是删除classmethod注解,直接分配参数。在这种情况下,调用将是:MyClass().dosomething(a)
  • @Rarblck 感谢您的跟进。特别感谢您解释 classmethod 概念。这对我很有帮助。根据您的建议,我更正了我的代码。但是,问题仍然存在。我什至无法实例化 MyClass()。
【解决方案2】:

我认为“MyClass”会被您按下运行时调用的 Choregraphe/PythonBridge 解释器动态替换。

正如您所见,每个 choregraphe box 类都被命名为“MyClass”,它们被生成的名称替换和更改,例如 root/class/boxname...

您可以尝试在 MyClass 中调用并打印 self.getName(),以获得线索。

所以在你的情况下,你可以:

  1. 直接在 myClass 中添加 doSomething
  2. 创建一个未后处理的,例如:

作为:

class MyVeryOneClass:
 def __init__(self):
    ...

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 2014-10-24
    • 2012-05-29
    • 2013-08-23
    • 2014-04-03
    • 2017-05-12
    • 2015-09-01
    相关资源
    最近更新 更多