Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Declaration of an array of unspecified size

Reply
Thread Tools

Re: Declaration of an array of unspecified size

 
 
Ulrich Petri
Guest
Posts: n/a
 
      09-01-2003
"Bertel Lund Hansen" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) net.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


 
Reply With Quote
 
 
 
 
Bertel Lund Hansen
Guest
Posts: n/a
 
      09-01-2003
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
 
Reply With Quote
 
 
 
 
Max M
Guest
Posts: n/a
 
      09-01-2003
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

 
Reply With Quote
 
Chad Netzer
Guest
Posts: n/a
 
      09-01-2003
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


 
Reply With Quote
 
Anton Vredegoor
Guest
Posts: n/a
 
      09-01-2003
"Ulrich Petri" <(E-Mail Removed)> 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
 
Reply With Quote
 
Bertel Lund Hansen
Guest
Posts: n/a
 
      09-01-2003
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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-01-2003

"Bertel Lund Hansen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) net.dk...
> 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


 
Reply With Quote
 
Ulrich Petri
Guest
Posts: n/a
 
      09-01-2003

"Anton Vredegoor" <(E-Mail Removed)> schrieb im Newsbeitrag
news:biv3jc$h3b$(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Incomplete array types (with unspecified size) ? Timothy Madden C++ 7 09-26-2010 06:38 AM
Pointer to array of unspecified size Grumble C Programming 2 01-12-2005 05:02 AM
Using HTTP to return unspecified size of data in response.. SPG Java 1 07-28-2004 12:15 PM
Pointer to array of unspecified size Christopher Benson-Manica C Programming 14 12-02-2003 02:33 PM
Re: Declaration of an array of unspecified size Bertrand Geston Python 1 08-31-2003 03:49 PM



Advertisments