[Tutor] How to use a str object, to find the class in exact name?

Steven D'Aprano steve at pearwood.info
Mon Mar 14 23:55:12 CET 2011

Yaşar Arabacı wrote:

> And What I want to do is a small economy application. It will work this 
> way:
> user add a product name, rate how much it like it over 10, and enter its 
> price.
> [repeated as many times as wanted]
> user will enter h(is|er) budget.
> user will use "calcute bundle" to show much of each product s?he should buy
> user will repeat any step as long as s?he likes.

My advice is not to mix the backend calculation engine with the frontend 
user interface in the same code. Keep them separate. So you should write 
  your classes and functions to:

* store products and prices
* calculate best buys for a budget
* etc.

And then have a separate function for the user interface, which handles:

* input and output to the user
* converting the user's text input to numbers, products, etc.
* calling the calculation functions
* printing the results

(The frontend and backend can be in the same file, but they should be 
separate functions or classes.)

At the moment, your code combines calculation with user-interface, which 
is a poor design:

class calculate:
     def bundle():
         print "your best bundle is -->"

You have the class responsible for calculating the bundle also 
responsible for displaying it to the user. It is better to separate 
those two functions, and have one class for calculating the bundle and 
returning it (not printing it!) and another function or class 
responsible for calling the calculate class with the user's input, and 
displaying the output to the user.

This will let you more easily change the user-interface, without needing 
to change the engine. Want to put your application in a GUI, or on a 
website? Change the user-interface parts, not the engine.

Hope this helps!


More information about the Tutor mailing list