Re: Declaration of an array of unspecified size

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

Bertel Lund Hansen

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

Max M

Bertel said:
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
 
C

Chad Netzer

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

Anton Vredegoor

Ulrich Petri said:
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
 
B

Bertel Lund Hansen

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

Terry Reedy

Bertel Lund Hansen said:
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
 
U

Ulrich Petri

Anton Vredegoor said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top