Declaration of an array of unspecified size
Ulrich Petri
ulope at gmx.de
Sun Aug 31 21:15:42 EDT 2003
"Bertel Lund Hansen" <nospamius at lundhansen.dk> schrieb im Newsbeitrag
news:m814lvc8h44vbd5g1m2iv05r33qg8ag9ta at news.stofanet.dk...
> 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.
>
> class PopMailServer:
> host = ""
> user = ""
> password = "*"
> mails = 0
> mail[] # This is wrong but what do I do?
mail = [] #would work
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[i]=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
More information about the Python-list
mailing list