Correct Way to Write in Python

Peter Otten __peter__ at web.de
Sat Aug 3 05:04:10 EDT 2013


Sagar Varule wrote:

> On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote:
>> punk.sagar at gmail.com wrote:

> Thanks a lot Peter. I appreciate your Help. You mentioned that C# code
> above is not good. If you can point me why it is not good, would help me
> learn new approaches as this type of Code I use to see long back(when i
> was fresher). There may be better approaches or concepts i am not aware
> of. If you can point me in that direction it would be gr8.

As I said, I don't know C#  -- but I already tried to fix some of the 
potential issues in my code snippet.

- A list is not the best choice to store the customers -- there should be a 
lookup by some kind of ID (I picked the name to keep it simple)
- Is it really necessary to expose that container in a language that 
provides "privacy"?
- Is there ever a customer without name/ID? I'd say no, so these should be 
passed as constructor arguments.
- Renaming a customer is a delicate process, you may need to keep track of 
the old name, the reason for the name, update your database etc., so I 
wouldn't allow setting the attribute and instead add a method

Bank.rename_customer(...)

or

Bank.customers.rename_customer(...)

Asking to translate code might make sense if you are a wizzard in the 
"other" language and want to see how a particular construct is written 
idomatically in Python, but to rewrite very basic C# code in Python is a bit 
like trying to learn a natural language by replacing one word after another 
in a text with the word in the new language that you looked up in a dict. 
The result tends to be underwhelming.

I recommend that you read the tutorial and then try to solve a simple task 
in Python. Whenever you run into a problem you can come here for help. 
Because there's a real problem behind your code there will be different ways 
to solve it in Python, and you'll learn much more about the possibilites 
Python has to offer while your code gradually becomes more idiomatic.




More information about the Python-list mailing list