Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'parameters': 0.04; 'syntax': 0.04; 'value,': 0.04; 'root': 0.05; 'subject:Python': 0.06; 'attribute': 0.07; 'dynamically': 0.07; 'explicit': 0.07; 'purpose.': 0.07; 'salary,': 0.07; 'classes.': 0.09; 'forcing': 0.09; 'function:': 0.09; 'hooks': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'uses.': 0.09; 'python': 0.11; '__init__,': 0.16; 'accepts': 0.16; 'attributes.': 0.16; 'calculates': 0.16; 'metaclasses.': 0.16; 're-usable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sorts': 0.16; 'subject:Classes': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; "python's": 0.19; 'retrieval': 0.19; 'addition,': 0.20; 'example': 0.22; 'separate': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'convenient': 0.24; 'class.': 0.26; 'define': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'properties': 0.29; 'related': 0.29; 'needed.': 0.30; 'operations,': 0.30; 'along': 0.30; 'serve': 0.31; 'writes:': 0.31; 'class': 0.32; 'cases': 0.33; 'programmers': 0.33; 'could': 0.34; 'classes': 0.35; 'objects': 0.35; 'but': 0.35; 'data,': 0.36; 'object,': 0.36; 'employee': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'little': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'number,': 0.60; 'most': 0.60; 'entire': 0.61; 'john': 0.61; 'simply': 0.61; 'simple': 0.61; 'provide': 0.64; 'here': 0.66; 'home': 0.69; 'square': 0.74; 'special': 0.74; 'address,': 0.75; '(probably': 0.84; 'calculations': 0.84; 'comparable': 0.84; 'usage.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Neil D. Cerutti" Subject: Re: Python Classes Date: Tue, 05 Aug 2014 11:37:29 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407253068 news.xs4all.nl 2959 [2001:888:2000:d::a6]:36005 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75742 On 8/4/2014 6:44 PM, John Gordon wrote: > In Shubham Tomar writes: > >> classes. I understand that you define classes to have re-usable methods and >> procedures, but, don't functions serve the same purpose. >> Can someone please explain the idea of classes > > If a function simply accepts some data, does some calculations on that > data and then returns a value, then you don't need classes at all. An > example of this might be the square-root function: pass it any number > and it calculates and returns the square root with no other data needed. > > But classes do come in handy for other sorts of uses. One classic example > is employees at a company. Each employee has a name, ID number, salary, > date of hire, home address, etc. > > You can create an Employee class to store those data items along with > methods to manipulate those data items in interesting ways. The data > items are specific to each separate Employee object, and the methods are > shared among the entire class. In simple cases like that, functions could do very well by including a little bundle of data (probably a dict) as one of the parameters for each related function. Classes help here by organizing the functions into namespaces, and allowing very convenient and explicit syntax for creating objects and using attributes. In addition, classes provide hooks into almost all of Python's syntax and operations, with special methods like __init__, __add__, etc. If you want your employees to be comparable using the <, >, == you need to use classes. Classes provide a means for objects to be related, substitutable, and interdependent, using inheritance. Properties work only with classes and provide a convenient way to customize attribute retrieval and setting without forcing a change in the syntax required for usage. Classes can be constructed dynamically using metaclasses. Some of these things can be emulated using just functions and mappings--it's what C programmers do--but most of classes in Python can do requires language support. -- Neil Cerutti