Declaration of an array of unspecified size

Bertrand Geston bergeston at yahoo.fr
Sun Aug 31 11:14:21 EDT 2003


"Bertel Lund Hansen" <nospamius at lundhansen.dk> a écrit dans le message 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?
>
>
> class PopMailServer:
>   host = ""
>   user = ""
>   password = "*"
>   mails = 0
>   mail[] # This is wrong but what do I do?
mail=[]
>
>   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.
        self.mail.append(pop.retr(i+1)[1])
>     pop.quit()
>     print "Antal mails: %d\n" % self.mails
>
> --
> Bertel
> http://bertel.lundhansen.dk/ FIDUSO: http://fiduso.dk/

Hi,

Above, in your code, the answer to your question.

I suggest this code (not tested - I didn't check neither the poplib - but
gives some ideas like instance variables, default args, ...):
class PopMailServer:
     def __init__ (self, host = "", user = "", password = "*"):
        self.host=host
        self.user=user
        self.password=password
        self.mails=[]
        pop=poplib.POP3(self.host)
        pop.user(self.user)
        pop.pass_(self.password)
        countMails=len(pop.list()[1])
        for i in range(countMails):
            self.mails.append(pop.retr(i+1)[1])
        pop.quit()
        print "Antal mails: %d\n" % len(self.mails)

bg






More information about the Python-list mailing list