Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'string': 0.09; '"__main__":': 0.09; '__name__': 0.09; 'assuming': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'concurrency': 0.16; 'name):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'value;': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; 'code': 0.31; 'void': 0.31; 'class': 0.32; 'running': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'doubt': 0.36; 'version:': 0.36; 'done': 0.36; 'list': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:o 30': 0.61; 'new': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'bank': 0.76; 'customer:': 0.84; 'moderately': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Correct Way to Write in Python Date: Sat, 03 Aug 2013 08:47:49 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508492b1.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375512468 news.xs4all.nl 15911 [2001:888:2000:d::a6]:60815 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51848 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 lstCustomers = new List(); > 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.