Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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
Python3.x:文件中的类无法在同一文件中找到其他类_Python_Class_Oop_Maya - Fatal编程技术网

Python3.x:文件中的类无法在同一文件中找到其他类

Python3.x:文件中的类无法在同一文件中找到其他类,python,class,oop,maya,Python,Class,Oop,Maya,我需要一些帮助来解决一个命名错误。值得注意的是,我正在Autodesk Maya 2017中执行此代码 下面是问题的概要:我不能在与我正在处理的类相同的文件中创建类的对象。因此,如果我定义了一个类foo和一个类foobar,并且在类foo中,我试图通过输入foobarObj=foobar来创建一个类foobar的对象,我会得到一个名称错误,表示foobar没有定义。 示例代码: class foo: foobarObj = foobar # crashes here (nameError

我需要一些帮助来解决一个命名错误。值得注意的是,我正在Autodesk Maya 2017中执行此代码

下面是问题的概要:我不能在与我正在处理的类相同的文件中创建类的对象。因此,如果我定义了一个类foo和一个类foobar,并且在类foo中,我试图通过输入foobarObj=foobar来创建一个类foobar的对象,我会得到一个名称错误,表示foobar没有定义。 示例代码:

class foo:
    foobarObj = foobar # crashes here (nameError: foobar is not defined)
    __init__():
       pass
class foobar:
    fooObj = foo # crashes here as well
    __init__():
       pass
我完全可能不理解python中的OOP,我的错误在那个示例代码中很明显。我不确定是否需要在foo之前定义foobar?如果是这样,我如何才能使代码工作?我需要在第二个类中创建第一个类的对象,但在第一个类中也要创建第二个类的对象。这可能是问题所在,因为当我尝试在第一个类中创建对象时,python不知道第二个类已经定义

似乎解决了一个类似的问题,如果不是同一个问题的话,但我真的不理解其中发生了什么,也不理解提供的答案,因为它是针对Django的

但是,如果我提供的示例代码看起来像“犹太”,那么下面是更多信息和我正在运行的实际代码:

在我的实际项目中有三个文件:createUI.py,它调用一个函数来创建一个新窗口;suite_modules.py,它包含程序UI的方法;bb_modules.py,它包含两个类,其中包含UI方法和基本命令方法。当我尝试在basic commands类中创建UI类的对象时,bb_modules.py文件中会出现错误

createUI.py这是执行的第一个脚本,它导入了第二个脚本套件\u模块

# createUI.py #
import os
import maya.cmds as cmds

# store the user's script directory as variable 'usd'
usd = cmds.internalVar(usd=True)

# prep the import path
bb_modules = 'suite_modules'
bb_modules = os.path.join(usd, bb_modules)
# import the module
import suite_modules
reload (suite_modules)

# create a new window
def createFloatingWin():
    UI = suite_modules.SuiteUIcommands()
    UI.createBuddySuiteUIWindow()
# bb_modules.py #
import maya.cmds as cmds
import os
import maya.mel as mel

class BBbatchCommands:
    # Define Variables
    # Get the UIcommands
    BBUI = BBUIcommands() # THE CRASH HAPPENS HERE #

    def __init__(self):
        self.uselessVariable = 0

    # there is more to the class but it never gets that far

class BBUIcommands:
    # Import the other module in this file, the batch commands
    BBcmd = BBbatchCommands() # THE CRASH HAPPENS HERE IF 
    # THE CLASSES ARE DEFINED IN REVERSE

    def __init__(self):
        pass

    # as usual, there is more to the class but it isn't relevant
suite_modules.py这是第一个脚本导入的模块。它导入第三个模块bb_modules.py

# suite_modules.py #
### Imports and script properties ###
import maya.cmds as cmds
import os
class SuiteUIcommands():
# Imports
# store the user's script directory as variable 'usd'
usd = cmds.internalVar(usd=True)
# Prep the file path for bb_modules
bb_modules = 'bb_modules'
bb_modules = os.path.join(usd, bb_modules)

# Import batch buddy commands
import bb_modules
reload (bb_modules)
# there is more to the script but it is not relevant, 
# the execution crashes during this import
bb_modules.py在从suite_模块导入期间,这是执行崩溃的地方

# createUI.py #
import os
import maya.cmds as cmds

# store the user's script directory as variable 'usd'
usd = cmds.internalVar(usd=True)

# prep the import path
bb_modules = 'suite_modules'
bb_modules = os.path.join(usd, bb_modules)
# import the module
import suite_modules
reload (suite_modules)

# create a new window
def createFloatingWin():
    UI = suite_modules.SuiteUIcommands()
    UI.createBuddySuiteUIWindow()
# bb_modules.py #
import maya.cmds as cmds
import os
import maya.mel as mel

class BBbatchCommands:
    # Define Variables
    # Get the UIcommands
    BBUI = BBUIcommands() # THE CRASH HAPPENS HERE #

    def __init__(self):
        self.uselessVariable = 0

    # there is more to the class but it never gets that far

class BBUIcommands:
    # Import the other module in this file, the batch commands
    BBcmd = BBbatchCommands() # THE CRASH HAPPENS HERE IF 
    # THE CLASSES ARE DEFINED IN REVERSE

    def __init__(self):
        pass

    # as usual, there is more to the class but it isn't relevant
最后,这里是Maya的完整堆栈跟踪输出:

# Error: line 1: name 'BBUIcommands' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File 
"C:/Users/censored/Documents/maya/2017/scripts\BuddySuite.py", line 21, in <module>
#     import suite_modules
#   File 
"C:/Users/censored/Documents/maya/2017/scripts\suite_modules.py", line 22, in <module>
#     class SuiteUIcommands():
#   File 
"C:/Users/censored/Documents/maya/2017/scripts\suite_modules.py", line 31, in SuiteUIcommands
#     import bb_modules
#   File 
"C:/Users/censored/Documents/maya/2017/scripts\bb_modules.py", line 13, in <module>
#     class BBbatchCommands:
#   File 
"C:/Users/censored/Documents/maya/2017/scripts\bb_modules.py", line 16, in BBbatchCommands
#     BBUI = BBUIcommands()
# NameError: name 'BBUIcommands' is not defined #`

任何帮助都将不胜感激,干杯

问题在于,在第一次导入模块时,顶级Python代码和类主体是按顺序执行的。引用任何尚未定义的名称将引发NameError

一个简单的解决办法是推迟派遣:

class BBbatchCommands:
    def __init__(self):
        self.uselessVariable = 0

class BBUIcommands:
    BBcmd = BBbatchCommands()

BBbatchCommands.BBUI = BBUIcommands()
没有撞车

然而,代码仍然有点奇怪,这种循环依赖在我看来并不正确。

类中的所有内容实际上都是python中的一个模块,因此它实际上是在解析脚本时执行的,就像执行导入一样

在类主体中定义变量时,您定义的是在类的所有实例之间共享的静态变量。只要我们有我们想要的行为,这是完全好的。类作为变量的简单名称空间也是很正常的

为了说明区别:

静态变量:

class Batch:
    commands = []

a = Batch()
b = Batch()
a.commands.append("a")
b.commands.append("b")
print(a.commands)
print(b.commands)
class Batch:
    def __init__(self):
        self.commands = []

a = Batch()
b = Batch()
a.commands.append("a")
b.commands.append("b")
print(a.commands)
print(b.commands)
将打印:

['a', 'b']
['a', 'b']
['a']
['b']
实例变量:

class Batch:
    commands = []

a = Batch()
b = Batch()
a.commands.append("a")
b.commands.append("b")
print(a.commands)
print(b.commands)
class Batch:
    def __init__(self):
        self.commands = []

a = Batch()
b = Batch()
a.commands.append("a")
b.commands.append("b")
print(a.commands)
print(b.commands)
将打印:

['a', 'b']
['a', 'b']
['a']
['b']
如果您正在创建BBbatchCommands类的多个实例,我会担心


_uuuinit_uuu中设置的所有内容对于实例都是唯一的。

类声明的顺序很重要。如果foobar还没有被声明,foo类怎么知道foobar是什么呢?但实际上你的设计已经失败了。为什么它们都需要对方的实例?为什么他们需要达到班级水平?至少,只有一个需要引用另一个。或者,在模块级别定义这两个实例,然后它们都可以访问它们。另外,如果这在Maya 2017中运行,则是Python 2.7.x,而不是3.x。此解决方案要求在类中定义BBUI变量,如下所示:BBUI=None,对吗?因此,解决方案是创建一个空变量,然后在我定义这两个类之后用对象填充它?@DesignRunner不需要BBUI=None,尽管您可以将它放在那里。Python对象和类的属性和变量可以在任何时候任何地方添加。循环依赖关系的存在是因为它们都是不同的模块,一个类中的函数需要访问另一个类模块中包含的函数。基本上,这是一个非OOP项目,最近被重新格式化为使用类。当我最初设计方法/函数时,我没有计划将它们放在类中,因此当我创建类时,我不得不采取一种相当愚蠢的方法使流仍然工作。我知道这不是最佳实践,将来也会避免,但我仍然需要这种特定的设计来工作。安蒂·哈帕拉:哇,我不知道,python总是让我大吃一惊。谢谢大家的帮助!现在可以了。