Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51848
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Correct Way to Write in Python |
| Date | 2013-08-03 08:47 +0200 |
| Organization | None |
| References | <daaa64d5-3366-48b4-980c-cd91a440aa90@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.149.1375512468.1251.python-list@python.org> (permalink) |
punk.sagar@gmail.com wrote:
> Hi All,
>
> Im new to Python. Im coming from C# background and want to learn Python.
> I was used to do following thing in C# in my previous experiences. I want
> to know how do I implement below example in Python. How these things are
> done in Python.
> [code]
> public class Bank
> {
>
> public List<Customer> lstCustomers = new List<Customer>();
> private string micrcode;
>
> public void Bank()
> {
> customer
> }
>
> }
>
> public class Customer
> {
> private srting customername;
>
> public string CustomerName
>
> {
> get { return customername; }
> set { customername = value; }
> }
> }
>
> main()
> {
> Customer objCustomer = new Customer;
> objCustomer.CustomerName = "XYZ"
>
> Bank objBank = new Bank();
> objBank.lstCustomer.Add(objCustomer);
>
> }
> [/code]
While I don't know C# I doubt that this is good C# code ;)
Here's a moderately cleaned-up Python version:
class DuplicateCustomerError(Exception):
pass
class Customer:
def __init__(self, name):
self.name = name
class Bank:
def __init__(self):
self.customers = {}
def add_customer(self, name):
if name in self.customers:
raise DuplicateCustomerError
customer = Customer(name)
self.customers[name] = customer
return customer
if __name__ == "__main__":
bank = Bank()
bank.add_customer("XYZ")
I'm assuming a tiny bank where every customer has a unique name and only one
program is running so that you can ignore concurrency issues.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Correct Way to Write in Python punk.sagar@gmail.com - 2013-08-02 23:18 -0700
Re: Correct Way to Write in Python Peter Otten <__peter__@web.de> - 2013-08-03 08:47 +0200
Re: Correct Way to Write in Python Sagar Varule <punk.sagar@gmail.com> - 2013-08-03 00:09 -0700
Re: Correct Way to Write in Python Peter Otten <__peter__@web.de> - 2013-08-03 11:04 +0200
Re: Correct Way to Write in Python Sagar Varule <punk.sagar@gmail.com> - 2013-08-03 09:07 -0700
Re: Correct Way to Write in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-03 08:20 +0000
Re: Correct Way to Write in Python Sagar Varule <punk.sagar@gmail.com> - 2013-08-03 08:59 -0700
Re: Correct Way to Write in Python Chris Angelico <rosuav@gmail.com> - 2013-08-03 17:21 +0100
csiph-web