Python:在B类中从A类调用方法A? [英] Python: Calling method A from class A within class B?

查看:1138
本文介绍了Python:在B类中从A类调用方法A?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多类似的问题,但没有一个答案可以点击,所以请随身携带。



我在努力使用Python学习OOP,但我一直跑错误(像这样),只是让我认为这是无意义的,只是使用方法更容易。



这里是我的代码:


 类TheGUI(wx.Frame):
def __init __(self,title,size):
wx.Frame .__ init __(self,None,1,title,size = size)

#GUI ...

textbox.TextCtrl(panel1,1,pos =(67,7),size =(150,20))
button1.Bind(wx.EVT_BUTTON,self.button1Click)

self.Show(True)

def button1Click(self,event):
#It需要做LoadThread函数!

class WebParser:

def LoadThread(self,thread_id):
#It需要获取文本框的内容!


TheGUI = TheGUI(Text RPG,(500,500))
TheParser = WebParser

TheApp.MainLoop()


所以我的问题是GUI类需要使用一个在WebParser类中,WebParser类需要从GUI类中存在的文本框中获取文本。



我知道我可以通过传递对象参数,但是这看起来完全没有意义,必须有一个更合乎逻辑的方式来做这个不使用类似乎没有意义吗?



提前感谢!

解决方案

您似乎有几个误解。


  1. 在这里使用方法。 (


  2. 类不是某种功能的容器或命名空间。他们定义数据类型。具有类WebParser的意义在于您可以具有多个WebParser 。


  3. 在Python中,类也是对象。现在,你不是 TheParser WebParser ;你使它成为类本身的别名。换句话说, TheParser 现在等于网络解析器的概念,或多或少。


  4. 传递参数不是无意义;它是如何在函数之间获取信息。你不会(通常!)传递类;你传递类的实例。无论您是否使用课程,您都会有根本上相同的沟通问题。


。首先,需要创建WebParser,所以我们通过使用它的构造函数来处理。接下来,我们的实例 TheGUI (一个糟糕的名字btw - 该不真正属于)需要知道我们的实例,所以我们将它传递给构造函数。这样,实例可以保留对它的引用,并使用它来调用 LoadThread 方法。它还需要保存对文本框的引用,以便此信息可以传递到 LoadThread

它看起来像:

  class MyGUI(wx.Frame):
def __init __(self,title,size,loader):
wx.Frame .__ init __(self,None,1,title,size = size)

# ...

textbox.TextCtrl(panel1,1,pos =(67,7),size =(150,20))
self.textbox = textbox
button1。 Bind(wx.EVT_BUTTON,self.button1Click)
self.loader = loader
self.Show(True)

def button1Click(self,event):
self .loader.LoadThread(get_thread_id(),self.textbox)


class WebParser:
def LoadThread(self,thread_id,a_textbox):
do_something_with(a_textbox)


TheGUI = MyGUI(Text RPG,(500,500),WebParser())

TheApp.MainLoop()


There are a number of questions that are similar to this, but none of the answers hits the spot - so please bear with me.

I am trying my hardest to learn OOP using Python, but i keep running into errors (like this one) which just make me think this is all pointless and it would be easier to just use methods.

Here is my code:

class TheGUI(wx.Frame):
    def __init__(self, title, size):
        wx.Frame.__init__(self, None, 1, title, size=size)

        # The GUI is made ...

        textbox.TextCtrl(panel1, 1, pos=(67,7), size=(150, 20))
        button1.Bind(wx.EVT_BUTTON, self.button1Click)

        self.Show(True) 

    def button1Click(self, event):
        #It needs to do the LoadThread function!

class WebParser:

    def LoadThread(self, thread_id):
        #It needs to get the contents of textbox!


TheGUI = TheGUI("Text RPG", (500,500))
TheParser = WebParser

TheApp.MainLoop()

So the problem i am having is that the GUI class needs to use a function that is in the WebParser class, and the WebParser class needs to get text from a textbox that exists in the GUI class.

I know i could do this by passing the objects around as parameters, but that seems utterly pointless, there must be a more logical way to do this that doesn't using classes seem so pointless?

Thanks in advance!

解决方案

You seem to have several misconceptions.

  1. You are using methods here. (Did you mean "functions", as in not belonging to a class?)

  2. Classes are not some kind of container or namespace for functionality. They define a data type. The point of having the class WebParser is that you can have more than one WebParser.

  3. In Python, classes are objects too. Right now, you aren't making TheParser be a WebParser; you are making it an alias for the class itself. In other words, TheParser is now equal to "the concept of a web parser", more or less.

  4. Passing parameters around is not "pointless"; it's how you get information between functions. You don't (normally!) pass classes around; you pass around instances of the classes. Whether you use classes or not, you will have fundamentally the same communication problem.

It is very simple to fix this. First off, the WebParser needs to be created, so we take care of that by using its constructor. Next, our instance of TheGUI (a poor name btw - "the" doesn't really belong) needs to know about our instance, so we pass it to the constructor. That way, the instance can keep a reference to it, and use it to call the LoadThread method. It also needs to keep a reference to the textbox, so that this information can be passed along to LoadThread.

It looks like:

class MyGUI(wx.Frame):
    def __init__(self, title, size, loader):
        wx.Frame.__init__(self, None, 1, title, size=size)

        # The GUI is made ...

        textbox.TextCtrl(panel1, 1, pos=(67,7), size=(150, 20))
        self.textbox = textbox
        button1.Bind(wx.EVT_BUTTON, self.button1Click)
        self.loader = loader
        self.Show(True) 

    def button1Click(self, event):
        self.loader.LoadThread(get_thread_id(), self.textbox)


class WebParser:
    def LoadThread(self, thread_id, a_textbox):
        do_something_with(a_textbox)


TheGUI = MyGUI("Text RPG", (500,500), WebParser())

TheApp.MainLoop()

这篇关于Python:在B类中从A类调用方法A?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆