Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Torrie Newsgroups: comp.lang.python Subject: Re: When I need classes? Date: Sun, 10 Jan 2016 08:02:41 -0700 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 5gwLsHt88RVHt/TzyfSYzQPk+FgAVxgDEn64NxOKymNQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'needed,': 0.05; '__name__': 0.07; 'classes.': 0.07; '-based': 0.09; 'logic': 0.09; 'oop,': 0.09; 'scripts,': 0.09; 'shame': 0.09; 'variables,': 0.09; 'python': 0.10; "'__main__':": 0.16; 'already,': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scripts.': 0.16; 'singleton': 0.16; 'wrote:': 0.16; 'later': 0.16; 'subject:need': 0.18; 'python?': 0.18; 'variable': 0.18; 'oriented': 0.22; 'am,': 0.23; 'code.': 0.23; 'defined': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'module': 0.25; 'script': 0.25; 'header:User- Agent:1': 0.26; 'example': 0.26; 'define': 0.27; 'question': 0.27; 'classes': 0.30; 'code': 0.30; 'class.': 0.30; 'another': 0.32; "can't": 0.32; 'possibly': 0.32; 'instances': 0.33; "i'll": 0.33; 'message-id:@gmail.com': 0.34; 'friends,': 0.35; 'mix': 0.35; 'quite': 0.35; 'but': 0.36; 'there': 0.36; 'development.': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'shared': 0.38; 'speak': 0.38; 'end': 0.39; 'received:192': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'ever': 0.60; 'share': 0.61; 'charset:windows-1252': 0.62; 'between': 0.65; 'state,': 0.66; 'here': 0.66; 'real-life': 0.84; 'subject:When': 0.84; 'apparent': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101434 On 01/10/2016 12:29 AM, Arshpreet Singh wrote: > Hello Friends, I am quite new to OOP(object oriented Programming), I > did some projects with python which includes Data-Analysis, Flask Web > Development and some simple scripts. > > I have only one question which is bothering me most of the time, When > I will get the need to use Classes in Python? Or in other way is > there any real-life example where you can tell me this > problem/solution is kind of impossible in Python without Classes? I can't speak to Flask or any other web development. But for simple scripts, I'll start out with no classes. Just functions as needed, and some main logic. I always use the idiom: if __name__ == '__main__': # do main logic here This way I can import functions defined in this script into another script later if I want. If I find I need to share state between functions, and if I find that I might need to have multiple situations of shared state possibly at the same time, then I will refactor the script into a class. Despite the apparent shame of using global variables, if I need to share state between functions, but I cannot ever foresee a time when I'll need to have multiple instances of that state, then I'll just use a module-level global variable and continue to use normal functions, rather than define a class. In the parlance of OOP, this use case would be referred to as a "singleton" and a python module (a script) is a form of singleton already, even without using classes. In the end my code often is a mix of classes and non-class -based code.