Reset static variables or a workaround
Hi Guys, I have a custom user form class, it inherits my own custom Form class: class UserForm(Form): first_name = TextField(attributes={id='id_firstname'}) Now, everytime UserForm() is instantiated it saves the attributes of each form members and passes it on to the new instance. I understand this is because first_name is static in nature. But I would like to reset the first_name for every instance? How can I do this? Regards, Nav -- http://mail.python.org/mailman/listinfo/python-list
Newbie help- Can multiple instances with multiple names automatically created.
I have a class of let's say empty bottle which can have a mix of two items. I want to create let's say 30 of these objects which will have names based on the 2 attributes (apple juice, beer, grape juice, beer, etc) that I provide from a list. All the objects are a mix of (1 of three alcohols) and (1 of 10 juices), so I don't want to go through typing in the names of all the objects (which would be totally stupid). I get problems if I try such as I can't assign to literal etc. If I make a list of names using the attributes. then equate names to objects. the list gets populated by the objects and the names disappear. I want the ability to be able to call up any object by its name and manipulate it and yet not have to assign the name manually. How can this be done? -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help- Can multiple instances with multiple names automatically created.
On Jan 4, 4:54 pm, Chris Rebert wrote: > On Mon, Jan 4, 2010 at 1:32 PM, Shawn Milochik wrote: > > You could put them in a dictionary with the key being the name, instead of > > a list. > > To illustrate that for the OP: > > name2drink = {} > for booze in liquors: > for juice in juices: > name = juice +" "+booze # or however you're naming them > drink = Bottle(booze, juice) > name2drink[name] = drink > > #example use > favorite = name2drink["apple wine"] > favorite.rating = 9/10 typing favorite = such and such is what I am trying to avoid. I want to be able to use the name 'apple_wine' as the variable which has the object apple wine but not have to do this manually as you did with favorite. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help- Can multiple instances with multiple names automatically created.
Thanks Jan, You read my mind. That is exactly what I needed. Thanks for showing the product function from itertools as well. It seems easier to grasp than the nested loops, I had been using. I noticed chopin.edu.pl. Are you a musician? Nav -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help- Can multiple instances with multiple names automatically created.
Okay, let me ask another question: When we create instances of objects by doing x = className () are we using globalnamespace? if yes then: if using globalnamespace is bad then why does every book or tutorial about python classes give the above style of assignment as an example? Second why do we use a dictionary to create something like this? I know how it works, but what is wrong with simply creating instances automatically? Once created the data for the instances is automatically saved in their own space? Why have a round about way using dictionaries? Is there an advantage or does it conflict with something else? Why not have for i in specialList: #I presume it would have to be special, because it can't be a problematic type i = className(whatever) > > > what are the risks of globalnamespace use > > You're unnecessarily tying your code to the implementation. > > > and what are the benefits? > > Absolutely none that using a dictionary doesn't also give you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help- Can multiple instances with multiple names automatically created.
Thanks for pointing it out Steve. The blog post doesn't explain it very well. I understand the risk of exec or eval(input). but what are the risks of globalnamespace use and what are the benefits? -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie help- Can multiple instances with multiple names automatically created.
"> if yes then: > if using globalnamespace is bad then why does every book or tutorial > about python classes give the above style of assignment as an > example? That's a basic assignment example. It's not a direct manipulation of globals(), like the solution given by Jan, which you seem to feel is the answer. And unless it happens outside of a nested scope like a class or function definition, it's not 'global'. " Thanks for clearing that up Alex. The reason I liked Jan's solution at first glance was that it allowed me to manipulate the objects directly after I created the class. But I am using Shawn's advice now because it lets me abstract, but still allows me to directly manipulate the object if I need to. @ Steven "No, you're confused -- the problem isn't with using the global namespace. The problem is that you don't know what names you want to use ahead of time. " Actually I know what the names would be and how I want to use them. If you look at my example in the beginning you will notice that it creates unique objects with unique names, which anyone who looks at what is being mixed can easily figure out and of course the number and names of objects is directly dependent on the mixed elements. (although perhaps bottle is not the best analogy because the contents can change, which will not change once the object is created.) Having a stable and descriptive naming strategy was important, especially to use the unique object directly, and to not have to go searching where they might be located at any moment, e.g. if they are being moved about as part of other data structures. If anyone has any other thoughts regarding this, I would appreciate them. - Thanks everyone. -- http://mail.python.org/mailman/listinfo/python-list