Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51849
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-08-03 00:09 -0700 |
| References | <daaa64d5-3366-48b4-980c-cd91a440aa90@googlegroups.com> <mailman.149.1375512468.1251.python-list@python.org> |
| Message-ID | <f5f5b848-005c-4aaa-bc96-49dbc3fe4583@googlegroups.com> (permalink) |
| Subject | Re: Correct Way to Write in Python |
| From | Sagar Varule <punk.sagar@gmail.com> |
On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote:
> 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.
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.
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