On Thu, Nov 17, 2011 at 1:17 PM, Mic <o0m...@hotmail.se> wrote: > I have now worked to stop using the global scope and instead put my > prior global variable into > the constructor in the class. I believe that I have managed to do that now. > > Do you believe that this is correctly done? > > > #Trying putting the_time'' in constructor instead of > #putting it in the global scope to shorten code. > > <snip> > def update_time(self): > self.display_time.config(text=time.strftime('%H:%M:%S'), font='40') > self.after(20, self.update_time) >
Since you're using the shorter version of the function you no longer need self.the_time, so you can go ahead and remove that variable. > > I have another question. > It would have been fine (and even preferred) to create a new email here with a title something like "How do I shorten this code?" > > Say that I have a class and I want to make 100 objects. > Then it could look like this: > <snip> > class Chairs(object): > <snip code> > > #Create the objects > chair1=Chairs("10","20") > chair2=Chairs("10","20") > chair3=Chairs("10","20") > > How do I shorten this? I have thought of using a for sling. I have looked > in my programming > book and on the internet, but I don’t know how to make this shorter. The > arguements (“10”, “20”) > should be the same for every object, which should make it easier than if > they were different each time? > If you ever write a line of code more than once, it's a good sign that you have what's called a code smell. This example is very smelly code ;) What you should do instead is have a collection of chairs: chairs = [] for _ in range(100): # the underscore `_` indicates that you don't care about the value chairs.append(Chairs("10","20)) You can do that even shorter with a list comprehension, but I'll leave that exercise up to you :) HTH, Wayne
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor