U
Ulrich Petri
Bertel Lund Hansen said:Hi all
I am relatively new to Python but have som programming
experience. I am experimenting wit a POP3-program and it's fairly
easy.
I want to read the mails into an array of lists so I later can
choose which one to display. But I need to declare an array of
unknown size before I can use it in the code. How do I manage
that?
You don't. Python doesn't have the concept of declaring variables.
mail = [] #would workclass PopMailServer:
host = ""
user = ""
password = "*"
mails = 0
mail[] # This is wrong but what do I do?
But you are creating class-variables here. Are you sure thats what you want?
def __init__ (self):
pop=poplib.POP3(self.host)
pop.user(self.user)
pop.pass_(self.password)
self.mails=len(pop.list()[1])
for i in range(self.mails):
self.mail=pop.retr(i+1)[1] # This is also wrong.
pop.quit()
print "Antal mails: %d\n" % self.mails
you can skip the "mails" variable. len(mail) will give the same.
An optimzed (untested) version:
class PopMailServer:
def __init__(self, host, user, pass):
self.host = host
self.user = user
self.password = pass
self.mail = []
def action(self):
pop = poplib.POP3(self.host)
pop.user(self.user)
pop.pass_(self.password)
for x in p.list()[1]:
num, len = x.split()
self.mail.append(pop.retr(num)[1])
pop.quit()
print "Total mails: %d" % len(self.mail)
of course this lacks exception handling...
HTH
Ciao Ulrich