0

更新:为了解决Choregraphe和 Python 的结合,我拒绝了拥有@classmethod. 相反,MyCustomClass当我想使用MyClass.

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

我使用 Nao 的 Python 框用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()时,我NameError上了MyClass

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

我该如何解决?

4

2 回答 2

0

首先,您的@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

于 2018-11-03T13:31:43.447 回答
0

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

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

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

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

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

作为:

class MyVeryOneClass:
 def __init__(self):
    ...
于 2018-11-06T14:58:55.353 回答