如何从 Python 中的方法访问函数 [英] How to Access Functions from Methods in Python

查看:48
本文介绍了如何从 Python 中的方法访问函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在同一个类中,如何从一个方法中访问一个函数?

Within the same class, how can one access a function from within a method?

背景

世界你好!我目前正在使用 Tkinter 开发一个 GUI IM 程序.要通过套接字发送消息,我需要从方法访问函数.我的 self.network 方法有一个名为 send_message() 的函数,我想访问它.

Hello World! I am currently working on a GUI IM program using Tkinter. To send messages over the socket, I need to access functions from methods. My self.network method has a function called send_message() which I want to access.

代码

为了访问它,我尝试使用此代码:

To access it, I have tried to use this code:

def messageFun(self):

    //Other widgets are here//

    self.send = Button (self.messageFrame, text = "Send",
                        command = self.network(send_message()))
    self.send.grid(row = 1, column = 1, sticky = S)

self.network方法和send_message函数如下:

def network(self):

    def send_message():   
            #Send some data to the remote server
            message = self.message.get("0.0",END)

            try:
                 #set the whole string
                 s.sendall(message)
            except socket.error:
            #Send Failed
                print "Send failed"
                sys.exit()

尽管我努力调试代码,但我总是收到这条消息的回报:

Despite my efforts to debug the code, I am always rewarded with this message:

Traceback (most recent call last):
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 178, in <module>
    app = Application(root)
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 73, in __init__
    self.messageFun()
  File "D:\Python Programs\Sockets\First Project\IM Project\Server GUI InDev.py", line 156, in messageFun
    command = self.network(send_message()))
NameError: global name 'send_message' is not defined

推荐答案

network(send_message()) 是一个奇怪的语法,试图触发函数 send_message()

network(send_message()) is a strange syntax to try to trigger the function send_message()

我建议采用这种方式:

def send_message(instance):   
        #Send some data to the remote server
        message = instance.message.get("0.0",END)

        try:
             #set the whole string
             s.sendall(message)
        except socket.error:
        #Send Failed
            print "Send failed"
            sys.exit()

def network(self,what,send_message=send_message):
    if what =='send message":
        send_message(self)


self.send = Button (self.messageFrame, text = "Send",
                    command = self.network("send message"))

要么是这个:

class NENE:
    def __init__(self):
        self.network.__dict__['send'] = send_message

    def send_message(self):   
        #Send some data to the remote server
        message = self.message.get("0.0",END)
        try:
             #set the whole string
             s.sendall(message)
        except socket.error:
        #Send Failed
            print "Send failed"
            sys.exit()

    def network(self):
        pass


self.send = Button (self.messageFrame, text = "Send",
                    command = self.network.send_message())

在这第二个代码中,我把 pass 放在 network() 函数中,因为我不知道你真正的函数的内容,但你会把这个内容代替这个pass

In this second code, I've put pass in the network() function because I don't know the content of your real function but you'll put this content in place of this pass

我已将这些方法放在一个 NENE 类中以展示它是如何工作的.因为 send_message() 必须是与 network() 是方法的实例相同的实例的方法.
然后,在每次创建新实例时,send_message() 也成为 network() 方法的一个属性.
因此,send_message() 完全是实例及其方法之一的属性;但没关系,除了它实现了你的目标:send_message() 现在是 network() 的一个属性,可以通过 network 属性,它知道它适用的实例是什么.

I've put the methods in a class NENE to show how it works. Because send_message() MUST be a method for the same instance as the instance of which network() is a method.
Then, at each creation of a new instance, send_message() is made an attribute of the network() method too.
Consequently, send_message() is altogether an attribute of the instance and of one of its methods; but it doesn't matter, except that it fulfills your aim: send_message() is now a an attribute of network() that may be called through network attribute and it knows what is the instance for which it works.

请原谅我英语不好

这篇关于如何从 Python 中的方法访问函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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