Re: Declaration of an array of unspecified size

Discussion in 'Python' started by Ulrich Petri, Sep 1, 2003.

  1. Ulrich Petri

    Ulrich Petri Guest

    "Bertel Lund Hansen" <> schrieb im Newsbeitrag
    news:...
    > 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=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
     
    Ulrich Petri, Sep 1, 2003
    #1
    1. Advertising

  2. Ulrich Petri skrev:

    >> 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?


    Yes. I have the habit from Java and C++. Is there a better way to
    declare them in Python?

    I am using the class for inheritance. Does that matter?

    In my present version i deleted the lines where I declared empty
    attributes. It seems to work anyway.

    >> self.mails=len(pop.list()[1])

    >you can skip the "mails" variable. len(mail) will give the same.


    Yes, I saw that in another posting. But I have a habit that I
    aquired working with slow computers: always to substitute
    function-calls with variables (faster). Is that a bad habit?

    --
    Bertel, Denmark
     
    Bertel Lund Hansen, Sep 1, 2003
    #2
    1. Advertising

  3. Ulrich Petri

    Max M Guest

    Bertel Lund Hansen wrote:

    > Ulrich Petri skrev:


    >>>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?

    >
    > Yes. I have the habit from Java and C++. Is there a better way to
    > declare them in Python?



    Class variables holds the same value for all instances of your object.
    If you want different instances to be able to have different values you
    should declare them as instance variables in the __init__ method.

    class PopMailServer:

    def __init__(self):
    self.host = ""
    self.user = ""
    self.password = "*"
    self.mails = 0
    self.mail = []


    regards Max M
     
    Max M, Sep 1, 2003
    #3
  4. Ulrich Petri

    Chad Netzer Guest

    On Sun, 2003-08-31 at 23:43, Bertel Lund Hansen wrote:
    > Ulrich Petri skrev:


    > >> self.mails=len(pop.list()[1])

    > >you can skip the "mails" variable. len(mail) will give the same.

    >
    > Yes, I saw that in another posting. But I have a habit that I
    > aquired working with slow computers: always to substitute
    > function-calls with variables (faster). Is that a bad habit?


    Yes, because of the word "always". If you mean that literally, then I
    would consider it VERY bad style (sorry; no offense meant). This habit
    can be quite error prone, just because it is possible, even common, for
    your length variable to become out of sync with the true list length.
    These kinds of bugs can be very difficult to track down.

    It is best to use this habit sparingly, such as when you know your
    algorithm won't change the list, and you use the line length several
    times in quick succession, or in a small loop. That kind of thing is
    done all the time, on a very limited and local scale, and isn't
    necessarily bad. But don't always do it just for the sake of doing it.
    Only do it where it is a known bottleneck (or you know it will remain
    coorect, and it makes the code easier to express)

    Python's lists are not 'linked lists', btw, and the length operation is
    very fast (O(1)), because the length is always known exactly, and can be
    gotten with a fast lookup (ie. it doesn't have to count). The function
    call overhead in python is a bit high, but presumably your program calls
    LOTS of functions; a few more len() calls will have virtually no effect,
    outside of loops.

    Finally, you can google for the term "memoize python" on Google, which
    will demonstrate a technique for automatically caching function call
    results, which you may find interesting.

    --
    Chad Netzer
     
    Chad Netzer, Sep 1, 2003
    #4
  5. "Ulrich Petri" <> wrote:

    >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...


    You followed the OP into using "pop" as a name for an instance.
    However "pop" is also a *method* of type list. While it's not as bad
    as for example using the name "str" for a string ("str" is a build-in
    function which is not reachable anymore if it's name is used this way)
    this still makes my spider sense tingle. Whether it's because you
    failed to mention this or just because it's dialectical I don't know
    :)

    Anton
     
    Anton Vredegoor, Sep 1, 2003
    #5
  6. Anton Vredegoor skrev:

    >You followed the OP into using "pop" as a name for an instance.
    >However "pop" is also a *method* of type list.


    I will change the name. I never use existing identifiers as
    variables.

    In fact people usually complain about my habit in this respect,
    because if I want an English name, but realize that it is already
    taken (by me or the system), I use a Danish one instead, so my
    program might look like this:

    if number==5: tal*=7
    if text=="people": udsagn="Okay"

    a.s.o.

    --
    Bertel, Denmark
     
    Bertel Lund Hansen, Sep 1, 2003
    #6
  7. Ulrich Petri

    Terry Reedy Guest

    "Bertel Lund Hansen" <> wrote in message
    news:...
    > Yes. I have the habit from Java and C++. Is there a better way to
    > declare them in Python?


    Python is neither Java nore C++ and there are significant differences.
    It requires different language specific habits.

    > Yes, I saw that in another posting. But I have a habit that I
    > aquired working with slow computers: always to substitute
    > function-calls with variables (faster). Is that a bad habit?


    Today's desktops literally match the supercomputers of enough years
    ago and make many slow-computer habits (like writing lots of
    assembler) generally obsolete. The fast computer paradigm: write
    something relatively simple that works. If not fast enough (and it
    often will be fast enough), then profile and optimize hotspots.

    Terry J. Reedy
     
    Terry Reedy, Sep 1, 2003
    #7
  8. Ulrich Petri

    Ulrich Petri Guest

    "Anton Vredegoor" <> schrieb im Newsbeitrag
    news:biv3jc$h3b$...
    >
    > You followed the OP into using "pop" as a name for an instance.
    > However "pop" is also a *method* of type list. While it's not as bad
    > as for example using the name "str" for a string ("str" is a build-in
    > function which is not reachable anymore if it's name is used this way)
    > this still makes my spider sense tingle. Whether it's because you
    > failed to mention this or just because it's dialectical I don't know


    Thanks. I missed that indeed (perhaps due to the time when i wrote that post
    ;).

    Cu Ulrich
     
    Ulrich Petri, Sep 1, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SPG
    Replies:
    1
    Views:
    1,256
    Rogan Dawes
    Jul 28, 2004
  2. Christopher Benson-Manica

    Pointer to array of unspecified size

    Christopher Benson-Manica, Nov 26, 2003, in forum: C Programming
    Replies:
    14
    Views:
    623
    Dan Pop
    Dec 2, 2003
  3. Bertrand Geston
    Replies:
    1
    Views:
    373
    Bertel Lund Hansen
    Aug 31, 2003
  4. Grumble

    Pointer to array of unspecified size

    Grumble, Jan 11, 2005, in forum: C Programming
    Replies:
    2
    Views:
    390
  5. Timothy Madden
    Replies:
    7
    Views:
    1,161
    Juha Nieminen
    Sep 26, 2010
Loading...

Share This Page