[英]Trying to understand Python classes and modules
I am working on a simple IMAP client written in Python.我正在开发一个用 Python 编写的简单 IMAP 客户端。 Once I got an initial script working (in that I could download and show the user a list of mailboxes), I decided it was time to get the code more organized.一旦我得到了一个初始脚本(因为我可以下载并向用户显示邮箱列表),我决定是时候让代码更有条理了。
I broke up the IMAP related functions into a module name imap.py and the user-interaction + CUI related functions into a module name mail.py.我将 IMAP 相关函数分解为模块名称 imap.py,将用户交互 + CUI 相关函数分解为模块名称 mail.py。 I imported imap.py into mail.py, but when I try to call the imap_functions class I get a "name not defined" error.我将 imap.py 导入到 mail.py 中,但是当我尝试调用 imap_functions class 时出现“未定义名称”错误。 I'm confused, because I define the class imap_functions at the beginning of imap.py, which is, as I said, imported into the main script.我很困惑,因为我在 imap.py 的开头定义了 class imap_functions,正如我所说,它是导入到主脚本中的。
Below is the code for the two scripts:下面是两个脚本的代码:
# mail.py
import py_cui
import imap
#from itertools import chain
host = "imap.gmail.com"
username = "user@gmail.com"
password = "someappapassword"
imap_functions = imap_functions(host, username, password)
class Mail:
def __init__(self, master):
self.master = master
# Creating widgets for mailbox list on left and message list onright
# Make Mailbox widget and get a list of folders for account
Mailboxes = self.master.add_scroll_menu('Mailboxes', 0, 0, row_span=6, column_span=1)
Mailboxes.add_item_list(imap_functions.get_folders())
# Make a Messages widget and get a list of messages based on the folder selected.
Messages = self.master.add_scroll_menu('Messages', 0, 1, row_span=6, column_span=4)
# First select the folder
# message_list = client....whatever method lists messages
# Messages.add_item_list(message_list)
# Create the CUI with 7 rows 6 columns (may change later), pass it to the wrapper object, and start it
root = py_cui.PyCUI(7, 6)
root.set_title('Mail')
s = Mail(root)
imap_functions.login()
root.start()
and和
### imap.py
import ssl
import imapclient
import email
class imap_functions:
def __init__(self, host, username, password):
self.host = host
self.username = username
self.password = password
self.client = IMAPClient(host)
def login(self, username, password):
"Handling various auth protocols"
#client.starttls() --- gmail does not support starttls
client.login(username, password)
return;
def get_folders(self):
"Grabbing the list of folders for the account"
get_folders = client.list_folders()
folder_list = []
for folders in get_folders:
folder_list.append(folders[2])
return folder_list;
#client.select_folder("INBOX")
You could either change the import to be您可以将导入更改为
from imap import imap_functions
# ...
imap_functions = imap_functions(host, username, password)
Or as pointed out by @mkrieger1 you can access the class from the module by calling或者正如@mkrieger1 所指出的,您可以通过调用从模块访问 class
import imap
# ...
imap_functions = imap.imap_functions(host, username, password)
I would also recommend changing the class name to ImapFunctions
since snake_case is usually used for variables and functions.我还建议将 class 名称更改为ImapFunctions
,因为 snake_case 通常用于变量和函数。 Ideally, you could change the name to something even nicer, like ImapClient
but I guess that's a matter of choice and who will see your code besides you.理想情况下,您可以将名称更改为更好的名称,例如ImapClient
,但我想这是一个选择问题,除了您之外,谁会看到您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.