Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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_Oop_Variables_Pygame - Fatal编程技术网

如何从另一个类python获取变量

如何从另一个类python获取变量,python,oop,variables,pygame,Python,Oop,Variables,Pygame,我怎样才能从另一个这样的类中得到一个变量呢 white=(255,255,255) class window: def open(a,b,c): displaysurf=pygame.display.set_mode(a) pygame.display.set_caption(b) displaysurf.fill(c) def close(): while True: for event i

我怎样才能从另一个这样的类中得到一个变量呢

white=(255,255,255)
class window:
    def open(a,b,c):
        displaysurf=pygame.display.set_mode(a)
        pygame.display.set_caption(b)
        displaysurf.fill(c)
    def close():
        while True:
            for event in pygame.event.get():
                if event.type==QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()
class img():
    def show(image,position):
        pygame.image.load(image)
        displaysurf.blit(image,position)

我想从
窗口获取
displaysurf
变量。open()
函数

除非使用return,否则无法访问函数中指定的变量。但是,如果变量是在类实例中分配的,那么您可以访问它,但有一定的限制

因此,
窗口
类将采用

class window(object):
    # initialize class variable
    def __init__(self, a, b, c):
        self.display_surf = pygame.display.set_mode(a)
        pygame.display.set_caption(b)
        self.display_surf.fill(c)

    def close(self):
        while True:
            for event in pygame.event.get():
                if event.type==QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()
open()
函数嵌入到
\uuuuu init\uuuuuuu()
函数中,因为
display\u surf
变量只分配了参数
a
,在这一点上,设置其他属性也是有意义的

现在要访问该变量,首先必须初始化该类。您可以在创建
img
类期间或之后执行此操作。最好在之后这样做,以获得更大的灵活性。因此,
img
类需要一个额外的参数来访问变量

class img(object):
    def show(win, img, pos):
        pygame.image.load(img)
        win.display_surf.blit(img, pos)

# set up and access
win = window( # a, b, c)
img.show(win, # img, pos)
我说限制的原因是,在使用变量之前必须先初始化
窗口
类,并且任何想要使用它的函数都必须将它作为参数之一


如果您不想将其作为函数的参数,则应将
win
变量设置为全局变量。但是,建议不要这样做,因为变量可能会在预期课程之外被篡改,并扰乱整个程序。

除非使用return,否则无法访问函数中指定的变量。但是,如果变量是在类实例中分配的,那么您可以访问它,但有一定的限制

因此,
窗口
类将采用

class window(object):
    # initialize class variable
    def __init__(self, a, b, c):
        self.display_surf = pygame.display.set_mode(a)
        pygame.display.set_caption(b)
        self.display_surf.fill(c)

    def close(self):
        while True:
            for event in pygame.event.get():
                if event.type==QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()
open()
函数嵌入到
\uuuuu init\uuuuuuu()
函数中,因为
display\u surf
变量只分配了参数
a
,在这一点上,设置其他属性也是有意义的

现在要访问该变量,首先必须初始化该类。您可以在创建
img
类期间或之后执行此操作。最好在之后这样做,以获得更大的灵活性。因此,
img
类需要一个额外的参数来访问变量

class img(object):
    def show(win, img, pos):
        pygame.image.load(img)
        win.display_surf.blit(img, pos)

# set up and access
win = window( # a, b, c)
img.show(win, # img, pos)
我说限制的原因是,在使用变量之前必须先初始化
窗口
类,并且任何想要使用它的函数都必须将它作为参数之一


如果您不想将其作为函数的参数,则应将
win
变量设置为全局变量。但是,建议不要这样做,因为该变量可能在预期课程之外被篡改,并扰乱整个程序。

您完全可以访问其他类中的变量。只需在变量名之前添加类名和函数名,每个名称之间用句点分隔-示例:

CLASSNAME.FunctionName.variableName = "abc" 
这将允许您从其他函数中访问变量,而无需将它们设置为全局变量、放在初始化之前或使用返回语句。首先是一个基本示例:

class CLASS1:
    def testingFunc():
        CLASS1.testingFunc.thisValue = 123

class CLASS2:
    def createValue():
        CLASS1.testingFunc()
        print(CLASS1.testingFunc.thisValue)

CLASS2.createValue()
以下是您的代码可能需要的外观,以使其在特定上下文中工作:

white=(255,255,255)
class window:
    def open(a,b,c):
        window.open.displaysurf=pygame.display.set_mode(a)
        pygame.display.set_caption(b)
        window.open.displaysurf.fill(c)

    def close():
        while True:
            for event in pygame.event.get():
                if event.type==QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()

class img():
    def show(image,position):
        pygame.image.load(image)
        window.open.displaysurf.blit(image,position)        

您可能希望编写另一个函数,在该函数中可以调用window类和img类,这样您就不会不必要地创建某个对象的额外实例。我不熟悉pygame,也不知道这段代码打算做什么,但我知道这个方法很有效,因为我一直都在使用它。

您完全可以访问其他类中的变量。只需在变量名之前添加类名和函数名,每个名称之间用句点分隔-示例:

CLASSNAME.FunctionName.variableName = "abc" 
这将允许您从其他函数中访问变量,而无需将它们设置为全局变量、放在初始化之前或使用返回语句。首先是一个基本示例:

class CLASS1:
    def testingFunc():
        CLASS1.testingFunc.thisValue = 123

class CLASS2:
    def createValue():
        CLASS1.testingFunc()
        print(CLASS1.testingFunc.thisValue)

CLASS2.createValue()
以下是您的代码可能需要的外观,以使其在特定上下文中工作:

white=(255,255,255)
class window:
    def open(a,b,c):
        window.open.displaysurf=pygame.display.set_mode(a)
        pygame.display.set_caption(b)
        window.open.displaysurf.fill(c)

    def close():
        while True:
            for event in pygame.event.get():
                if event.type==QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()

class img():
    def show(image,position):
        pygame.image.load(image)
        window.open.displaysurf.blit(image,position)        

您可能希望编写另一个函数,在该函数中可以调用window类和img类,这样您就不会不必要地创建某个对象的额外实例。我不熟悉pygame,也不知道这段代码打算做什么,但我知道这个方法很有效,因为我一直都在自己使用它。

你不能
displaysurf
仅在函数范围内存在。无法从
open()
方法调用它。欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。我建议您遵循关于类的教程,特别是实例和类变量。正如@zwer所说,修改
window.open()
以返回displaysurf。当然,这意味着无论您想在哪里获得该值,都需要一个有效的
窗口实例
displaysurf
仅在函数范围内存在。无法从
open()
方法调用它。欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。我建议您遵循关于类的教程,特别是实例和类变量。正如@zwer所说,修改
window.open()
以返回displaysurf。当然,这意味着无论您想在哪里获得该值,都需要一个有效的
窗口实例。