Re: [Tutor] Calling a function
Hi! I don't know if I'm right here, because I've tested a simple model of what you're trying to do: on Wed, 8 Jun 2005 23:45:59 -0700 Kevin Reeder <[EMAIL PROTECTED]> wrote : - Kevin Reeder > import time, makezeros Kevin Reeder > Kevin Reeder > def do_timing(num_times, *funcs): Here funcs is a tuple containing all remaining arguments of the functions. Kevin Reeder > totals = {} Kevin Reeder > for func in funcs: totals[func] = 0.0 Kevin Reeder > for x in range(num_times): Kevin Reeder > for func in funcs: Kevin Reeder > starttime = time.time() Kevin Reeder > apply(func) Kevin Reeder > stoptime = time.time() Kevin Reeder > elapsed = stoptime-starttime Kevin Reeder > totals[func] = totals[func] + elapsed Kevin Reeder > for func in funcs: Kevin Reeder > print "Running %s %d times took %.3f seconds" % Kevin Reeder > (func.__name__, num_times, totals[func]) Kevin Reeder > Kevin Reeder > do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply)) You call do_timing() with the number of calls and one Argument, the tuple of functions, so the funcs in do_timing() is a tuple containing your tuple on the call here. Calling it this way should solve it: do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply) Here is a short extract from the Python-Shell of my test: >>> def myfuncs( *funcs): ... for func in funcs: ... apply( func ) ... >>> def sayHello(): ... print "Hello\n" ... >>> myfuncs( sayHello, sayHello ) Hello Hello >>> myfuncs( ( sayHello, sayHello) ) Traceback (most recent call last): File "", line 1, in ? File "", line 3, in myfuncs TypeError: 'tuple' object is not callable --- end -- HTH Ewald ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling a function
Quoting Kevin Reeder <[EMAIL PROTECTED]>: > The second module is timings.py. > > import time, makezeros > > def do_timing(num_times, *funcs): > totals = {} > for func in funcs: totals[func] = 0.0 > for x in range(num_times): > for func in funcs: >starttime = time.time() >apply(func) >stoptime = time.time() >elapsed = stoptime-starttime >totals[func] = totals[func] + elapsed >for func in funcs: >print "Running %s %d times took %.3f seconds" % > (func.__name__, num_times, totals[func]) > > do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply)) Hi Kevin, I have two comments. First, a simple one: Using "apply" is no longer necessary in python. It is better to just call the function directly. (ie: replace 'apply(func)' with 'func()') Now, to your problem: You have (possibly) slightly misunderstood the "star" notation. Consider: >>> def printArgs(*args): ... print args ... >>> printArgs(1, 2, 'foo', 'bar') (1, 2, 'foo', 'bar') Basically, all the arguments to printArgs get collected into a tuple, which gets the name 'args' in this example. Here's another example: >>> def printArgs2(x, y, *args): ... print 'x:', x ... print 'y:', y ... print 'remaining:', args ... >>> printArgs2(1, 'foo', 3, 'hello world', 99) x: 1 y: foo remaining: (3, 'hello world', 99) In this case, x and y consumed the first two arguments to printArgs2, and the remainder went into the catch-all args. Now, lets look at the relevant bits of your code: def do_timing(num_times, *funcs): ... This says: "Call the first argument 'num_times', and put any other arguments into a tuple called 'funcs'." You then do this: do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply)) Here, you are calling do_timing with _two_ arguments. First argument: 100 Second argument: (makezeros.lots_of_appends, makezeros.one_multiply) Your second argument is a tuple with two elements. funcs in your function will be set to a tuple containing all remaining arguments --- ie: ((makezeros.lots_of_appends, makezeros.one_multiply),) (which is a tuple with one element, whose element is a tuple with two elements). You can correct this in two ways: - Change the function definition to: def do_timing(num_times, funcs): Now, funcs is just an ordinary positional argument, and it will take whatever you give it. If you give it a tuple of functions, then you can iterate over it the way you expect. or: - Change the function call to: do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply) *funcs will consume the two function arguments, giving you the same result as in the first option. My personal preference would be for the first option, which I think is clearer.. (it better expresses the idea that you have written a function which will take a list of functions and time them) HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding path to resource files in a GUI application
On Thu, 9 Jun 2005 08:52:59 +0200 Christian Meesters <[EMAIL PROTECTED]> wrote: > Hi > > Currently I'm writing a GUI application with wxPython (on OS X, but I > guess the problem is the same, regardless of the UNIX derivative one is > using). When I start the main script where it is located the > application finds all resource files (non-Python files like images and > html files for html windows) without any problem. However, if a put a > link in /usr/local/bin and start the script using the link the > application cannot find those resource files - unless, of course, I > will use full absolute paths to point to those files. One brief example > to illustrate the problem: > > The class Goals is in a file called Help.py, located in '/gui_lib/' as > seen from my main script. > > class Goals(wx.Frame): > def __init__(self,parent,frame,title,size,pos=wx.DefaultPosition): > wx.Frame.__init__(self,parent, > -1,title,size,style=wx.DEFAULT_FRAME_STYLE) > self.frame = frame > self.cwd = os.getcwd() > > self.html = HtmlWindow(self,-1) > self.html.LoadPage(os.path.join(self.cwd,'gui_lib/Goals.html')) > > #this, of course, won't work > #if the main script is called from somewhere else and not the > directory where the main script > #is located > > Any ideas what I could use instead of os.getcwd to construct a relative > path which will work even if I port the script to an other machine? > > Cheers > Christian > Hi Christian, try self.cwd = os.path.abspath(sys.path[0]) sys.path[0] is the directory of your main program file, with os.path.abspath() you can get rid of the "../../" stuff at the beginning of the path if the program is called from a link. I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling a function
Ewald & John, thanks for the help. i'll work on it some more with your ideas in mind (after getting some sleep). Kevin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling a function
Kevin Reeder said: >def do_timing(num_times, *funcs): Took me a while to work out what went wrong, but the first part occurs here. The *funcs bit means that any extra arguments passed to do_timing will be put into a tuple called funcs. So do_timing(100, dosomething, dosomethingelse) will pass the following arguments to the body of the function: num_times = 100 funcs = (dosomething, dosomethingelse) >totals = {} >for func in funcs: totals[func] = 0.0 You may want to recode this bit as well. I'll cover that in the PS. > for x in range(num_times): > for func in funcs: > starttime = time.time() > apply(func) > stoptime = time.time() > elapsed = stoptime-starttime > totals[func] = totals[func] + elapsed >for func in funcs: >print "Running %s %d times took %.3f seconds" % >(func.__name__, num_times, totals[func]) > >do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply)) And here's the next part of the problem. Instead of letting do_timing wrap the extra arguments up into a tuple for you: do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply) You are wrapping your two functions up as a tuple. So the do_timing function recieves the following when it starts: num_times = 100 funcs = ((makezeros.lots_of_appends, makezeros.one_multiply),) I.e. one entry which is a tuple. >Here's the outcome I get: > >$ python ./Python/timings.py >Traceback (most recent call last): > File "./Python/timings.py", line 17, in ? >do_timing(100, (lots_of_appends, one_multiply)) > File "./Python/timings.py", line 10, in do_timing >apply(func) >TypeError: 'tuple' object is not callable And that is why you get this TypeError. When you pass the first component of funcs to the apply(funcs) bit, you're asking python to do the following: apply((makezeros.lots_of_appends, makezeros.one_multiply)) Which python can't/won't do. *grins* P.S. You ask the code to set all total[funcs] = 0.0 Then you ask the code to calculate the timings of each function. Then you ask the code to print the timings for eac function. You're looping through the same data set three times. Even if you want the timings to print last, you can optimize you're code a little bit by doing the following: def do_timing(num_times, *funcs): totals = {} for func in funcs: totals[func] = 0.0 for x in range(num_times): starttime = time.time() apply(func) stoptime = time.time() elapsed = stoptime-starttime totals[func] = totals[func] + elapsed for func in funcs: print "Running %s %d times took %.3f seconds" % (func.__name__, num_times, totals[func]) Have fun! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding path to resource files in a GUI
On 9 Jun 2005, at 12:00, Michael Lange wrote: > Hi Christian, > > try > > self.cwd = os.path.abspath(sys.path[0]) > > sys.path[0] is the directory of your main program file, with > os.path.abspath() you can > get rid of the "../../" stuff at the beginning of the path if the > program is called from a link. > > I hope this helps It does! Actually I tried abspath, but didn't think of this very combination with sys.path ... Thanks a lot! Danke, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just a Python formatting question
Kristiano Don't worry about it. I'm sure everyone passed over this before. The identation errors are errors in the tabulation of a line Let's take your code in example >number=78 >guess=0 > >while guess != number: > guess=input ("Guess a number:") > if guess > number: > print "Too High" > elif guess < number: > print "Too low" The last line has to belong to the elif clause, right? so if you are working with a single tab or 4 spaces (default identation in Python) that means that for every loop statement, try , functions, classes or any line that ends with ":", the next line has to be 4 spaces forward. And all the lines that belong to the statement also. It does not matter if is a nested sequence. Let me lighten you: for i in somelist: do_something do_something_else if x==y: inside the if clause all statements go with 4 spaces after the if line elif x>y: the same here You ended an identation when you went back the spaces and typed elif so you don't acumulate the identations, you just keep using 4 spaces after the line with the ":" so Python knows what's inside and what's outside I hope to be clear, and if you don't understand it (probably because of my English) you can always read in the tutorial or in the Python help Best Regards Alberto > > print "Just right" > > >I just can't get it to run and I get some indentation error. > > I'm pretty new to this so I'm sorry if this question(s) sound(s) >amateurish. Do help. > >Thanks. >-Kristiano Ang >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor Gaucho ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] keylogger
You might want to write a keylogger because you have Children living in your house and you might want to protect them or yourself from their stupidity. (Ie downloading music illegally or giving out their address in a chat room, ideally the logger would sit there and send you an email if someone enters keys words like "music", "film", "your road name" etc. Does your mail provider give you FREE antivirus protection? Get Yahoo! Mail___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding path to resource files in a GUI application
> The class Goals is in a file called Help.py, located in '/gui_lib/' as > seen from my main script. > > class Goals(wx.Frame): > def __init__(self,parent,frame,title,size,pos=wx.DefaultPosition): > wx.Frame.__init__(self,parent, > -1,title,size,style=wx.DEFAULT_FRAME_STYLE) > self.frame = frame > self.cwd = os.getcwd() > > self.html = HtmlWindow(self,-1) > self.html.LoadPage(os.path.join(self.cwd,'gui_lib/Goals.html')) > Any ideas what I could use instead of os.getcwd to construct a relative > path which will work even if I port the script to an other machine? The normal way to do this on Unix is to use an environment variable. MYAPPHOME or somesuch that points to te root directory for your data. An alternative is to use a resource file .mapprc that lives in each users home directory and can thus be found with ~/.myapprc Within that file you define any settings you need including folder locations. Usually a combination of the above techniques is used, and a default myapprc(no dot) held someplace like /etc/myapp used if no environment variable is set or no valid local user file exists. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] learning how to use python
I am new to programming and this is the first language that I will be learning. I am trying to get the shell to print "hello world". I followed all of the steps to save it and then run the file to get it to print in the shell. the shell gives me an error and also says that personal firewall software is blocking the connection. does anyone have any suggestions on how i could fix this problem? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning how to use python
Hi ! This is the text idle says when you open it: Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. Is this your problem ? See ya ! Em Quinta 09 Junho 2005 16:50, [EMAIL PROTECTED] escreveu: > I am new to programming and this is the first language that I will be > learning. I am trying to get the shell to print "hello world". I followed > all of the steps to save it and then run the file to get it to print in the > shell. the shell gives me an error and also says that personal firewall > software is blocking the connection. does anyone have any suggestions on > how i could fix this problem? -- Douglas Soares de Andrade http://douglasandrade.cjb.net - dsa at unilestemg.br UnilesteMG - www.unilestemg.br ICQ, MSN = 76277921, douglas at tuxfamily.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newsreader list name?
I'm switching over from email digests to a newsreader and I can't find a reference for this tutor list. For example, I have the lists comp.lang.python and comp.lang.python.announce setup, but can't find something like comp.lang.python.tutor? There must be others on this list using a newsreader and I would appreciate the list name used. Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newsreader list name?
On 9 Jun 2005, [EMAIL PROTECTED] wrote: > I'm switching over from email digests to a newsreader and I can't > find a reference for this tutor list. > > For example, I have the lists comp.lang.python and > comp.lang.python.announce setup, but can't find something like > comp.lang.python.tutor? > > There must be others on this list using a newsreader and I would > appreciate the list name used. The server is: news.gmane.org and the group name is: gmane.comp.python.tutor In the WWW you can find the site of Gmane where all is explained. Gmane is great, since it hosts a lot of mailing lists. Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] keylogger
That is true. For what it's worth, I wouldn't write a keylogger in Python. C, C++ or Assembly would be the best option. On 6/10/05, David Holland <[EMAIL PROTECTED]> wrote: You might want to write a keylogger because you have Children living in your house and you might want to protect them or yourself from their stupidity. (Ie downloading music illegally or giving out their address in a chat room, ideally the logger would sit there and send you an email if someone enters keys words like "music", "film", "your road name" etc. Does your mail provider give you FREE antivirus protection? Get Yahoo! Mail -- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newsreader list name?
Thanks for the reply Karl. In the last couple days I have carefully read through the Gmane site several times. I'm on OS X 10.4.1 and downloaded Hogwasher to try. Using my ISPs news server I found the two lists I mentioned, but I can't get anything nntp://news.gmane.org alone or in combination with a group (e.g. + gmane.comp.python.tutor), or alone (server) with the group in the subscription window to work. Are you using Gmane successfully? Thanks Lee C On Jun 9, 2005, at 4:12 PM, Karl Pflästerer wrote: > On 9 Jun 2005, [EMAIL PROTECTED] wrote: > > >> I'm switching over from email digests to a newsreader and I can't >> find a reference for this tutor list. >> >> For example, I have the lists comp.lang.python and >> comp.lang.python.announce setup, but can't find something like >> comp.lang.python.tutor? >> >> There must be others on this list using a newsreader and I would >> appreciate the list name used. >> > > The server is: news.gmane.org > and the group name is: gmane.comp.python.tutor > > In the WWW you can find the site of Gmane where all is explained. > Gmane > is great, since it hosts a lot of mailing lists. > > >Karl > -- > Please do *not* send copies of replies to me. > I read the list > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Dynamically calling a class
example - list_of_classes = ['A', 'B', B', 'A'] class A: doingsomething class B: doing something for x in list_of_classes: x() my problem is that i get 'TypeError: 'str' object is not callable', of which i understand what the error is saying, i just want to know how to dynamically call the class. i have been trying to search google, but im not exactly sure what i should be searching for :-/ I'm not really asking for someone to hold my hand through it, just some pointers on where i need to start looking. thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamically calling a class
Quoting Ryan Parrish <[EMAIL PROTECTED]>: > example - > > list_of_classes = ['A', 'B', B', 'A'] > > class A: > doingsomething > class B: > doing something > > for x in list_of_classes: > x() > > my problem is that i get 'TypeError: 'str' object is not callable', of > which i understand what the error is saying, i just want to know how to > dynamically call the class. Just put the class object in the list! >>> class A: ... pass ... >>> class B: ... pass ... >>> class C: ... pass ... >>> classes = [A, B, C] >>> for cls in classes: ... x = cls() ... print x.__class__ ... __main__.A __main__.B __main__.C You can do some black magic to go from the string 'A' to the class A (eg, use eval()), but it's generally better to avoid that if you can. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Can't figure out syntax error
Hi there, I'm in the process of learning Python, and need some help deciphering the reason why the following code doesn't work: import sys, string def dec2bin(decNum): # validate the input as a postive integer number for char in decNum: if str(char).isdigit() == False: print "Not a postive integer number given when calling the dec2bin function." sys.exit() bin = ""# initialize the new binary result (as a string) num = int(decNum) if num == 0:# take care of the zero case bin = "0" while int(num) != 0:# the rest of the cases nextBin = int(num) % 2 # check if this column should be a 0 or 1 bin = str(nextBin) + bin# add the result to the front of the result string int(num) = int(num) / 2 # this is integer division, so we truncate the decimal part return bin # return the binary result # testing x = "13" print dec2bin(x) I get the following error: > File "convert.py", line 42 > int(num) = int(num) / 2 # this is integer division, so > we truncate the decimal part > SyntaxError: can't assign to function call Any help anyone can provide would be greatly appreciated. Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can't figure out syntax error
On Thu, 2005-06-09 at 18:16 -0600, [EMAIL PROTECTED] wrote: > Hi there, > > I'm in the process of learning Python, and need some help deciphering > the reason why the following code doesn't work: > int(num) = int(num) / 2 # this is integer division, so we truncate the > decimal part Here's your problem! "int(num)" will try to interpret "num" as an integer and return that (if possible) as an "int" object, but you're trying to assign it to the value of "int(num)/2" which doesn't make sense! What you want, probably, is: num = int(num) / 2 Here, "num" will be assigned the value (object actually) of the resulting of "int(num)/2" which will be an object of type "int". Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can't figure out syntax error
[EMAIL PROTECTED] wrote: > Hi there, > > I'm in the process of learning Python, and need some help deciphering > the reason why the following code doesn't work: Ziyad has answered your immediate question but I have a few more comments. > > import sys, string > > def dec2bin(decNum): > # validate the input as a postive integer number > for char in decNum: > if str(char).isdigit() == False: char is already a string so you don't have to call str(char), just use char directly: if char.isdigit() == False: but you don't have to compare to False, either, just say if not char.isdigit(): in fact you can call isdigit() on the whole string, it will test if all the characters are digits. So instead of the loop just write if not decNum.isdigit(): > print "Not a postive integer number given when calling > the dec2bin > function." > sys.exit() > > bin = ""# initialize the new > binary result (as a string) > num = int(decNum) OK, num is an integer now. > > if num == 0:# take care of the zero case > bin = "0" > > while int(num) != 0:# the rest of the cases > nextBin = int(num) % 2 # check if this column should > be a 0 or 1 > bin = str(nextBin) + bin# add the result to the > front of the result > string > int(num) = int(num) / 2 # this is integer division, so > we truncate > the decimal part You don't have to keep saying int(num), num is already an int and dividing by 2 gives a new int. You can just use num directly. > > return bin # return the > binary result > > # testing > x = "13" > print dec2bin(x) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Word COM interface
Hello, Does anyone know how to get the Microsoft Word 11.0 Object library working in ActiveState Python 2.4.1? When I go into PythonWin and try to load it under makePy, I get the following error: >>> Generating to >>> C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3.py Failed to execute command: from win32com.client import makepy;makepy.main() Traceback (most recent call last): File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\toolmenu.py", line 103, in HandleToolCommand exec "%s\n" % pyCmd File "", line 1, in ? File "C:\Python24\Lib\site-packages\win32com\client\makepy.py", line 362, in main GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel, bForDemand = bForDemand, bBuildHidden = hiddenSpec) File "C:\Python24\Lib\site-packages\win32com\client\makepy.py", line 273, in GenerateFromTypeLibSpec gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor) File "C:\Python24\Lib\site-packages\win32com\client\gencache.py", line 550, in AddModuleToCache mod = _GetModule(fname) File "C:\Python24\Lib\site-packages\win32com\client\gencache.py", line 629, in _GetModule mod = __import__(mod_name) File "C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3\__init__.py", line 2831 '{00020960---C000-0046}' : 'Pane', '{00020961---C000-0046}' : 'Windows', ^ SyntaxError: invalid syntax Any clue? -- Best regards, Chuck ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can't figure out syntax error
Phew -- thanks, Ziyad. That did the trick all right. In my frustration to figure out the problem, I just began explicitly type-casting as many variables as I could, and missed the fact that I had done the same to this as well. Thanks again, Dan On 9-Jun-05, at 7:32 PM, ZIYAD A. M. AL-BATLY - [EMAIL PROTECTED] wrote: > On Thu, 2005-06-09 at 18:16 -0600, [EMAIL PROTECTED] > wrote: >> Hi there, >> >> I'm in the process of learning Python, and need some help deciphering >> the reason why the following code doesn't work: > >> int(num) = int(num) / 2 # this is integer division, so we >> truncate the decimal part > Here's your problem! "int(num)" will try to interpret "num" as an > integer and return that (if possible) as an "int" object, but you're > trying to assign it to the value of "int(num)/2" which doesn't make > sense! > > What you want, probably, is: > num = int(num) / 2 > > Here, "num" will be assigned the value (object actually) of the > resulting of "int(num)/2" which will be an object of type "int". > > Ziyad. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling a function
On Jun 9, 2005, at 07:45, Kevin Reeder wrote: > I'm having trouble with this code which is meant to run a time > comparison between two similar functions. The first module is > makezeros.py > > def lots_of_appends(): > zeros = [] > for i in range(1): > zeros.append(0) > > def one_multiply(): > zeros = [0] * 1 Note that your functions don't do the same thing. The first one builds the list properly, but the second builds a list out of references to the same object -- that is, the 1 zeros in your resulting second list are actually the same object. As long as you're working with immutable types (numbers, strings) you won't notice there's a problem, but consider the following: >>> a = [[0]] * 10 >>> a[0].append(1) >>> a [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]] [0] is a list -- therefore, a mutable object: you can apply modifications to it. So, the list is actually a list of ten references to the same object -- [0]. When you run one of its methods that modifies it, since all references point to it, all members of the list seem to be modified as well -- but they're actually one single object that's present 10 times in the list. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor