Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101434

Re: When I need classes?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Michael Torrie <torriem@gmail.com>
Newsgroups comp.lang.python
Subject Re: When I need classes?
Date Sun, 10 Jan 2016 08:02:41 -0700
Lines 32
Message-ID <mailman.3.1452438172.3151.python-list@python.org> (permalink)
References <c0bb7499-c359-4010-aac2-270d1b82a47d@googlegroups.com>
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 <torriem+gmail@torriefamily.org>
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 <c0bb7499-c359-4010-aac2-270d1b82a47d@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101434

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

When I need classes? Arshpreet Singh <arsh840@gmail.com> - 2016-01-09 23:29 -0800
  Re: When I need classes? Michael Torrie <torriem@gmail.com> - 2016-01-10 08:02 -0700
    Re: When I need classes? Arshpreet Singh <arsh840@gmail.com> - 2016-01-10 22:45 -0800
      Re: When I need classes? Cameron Simpson <cs@zip.com.au> - 2016-01-11 18:27 +1100
  Re: When I need classes? Steven D'Aprano <steve@pearwood.info> - 2016-01-11 02:39 +1100
    Re: When I need classes? Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-10 15:48 -0200
      Re: When I need classes? Arshpreet Singh <arsh840@gmail.com> - 2016-01-10 22:24 -0800
    Re: When I need classes? Arshpreet Singh <arsh840@gmail.com> - 2016-01-10 22:28 -0800
    Re: When I need classes? Travis Griggs <travisgriggs@gmail.com> - 2016-01-11 15:45 -0800
      Re: When I need classes? Mike S <mscir@yahoo.com> - 2016-01-13 22:27 -0800
    Re: When I need classes? Michael Torrie <torriem@gmail.com> - 2016-01-11 16:59 -0700
    Re: When I need classes? Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-11 22:53 -0200
    Re: When I need classes? Chris Angelico <rosuav@gmail.com> - 2016-01-12 12:28 +1100
      Re: When I need classes? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-01-13 10:01 +1300
        Re: When I need classes? Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 19:08 -0200
    Re: When I need classes? Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-12 17:18 -0700
      Re: When I need classes? Steven D'Aprano <steve@pearwood.info> - 2016-01-13 12:33 +1100
  Re: When I need classes? Rustom Mody <rustompmody@gmail.com> - 2016-01-12 19:36 -0800
    Re: When I need classes? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-01-13 16:27 +1100
      Re: When I need classes? Rustom Mody <rustompmody@gmail.com> - 2016-01-12 22:31 -0800
        Re: When I need classes? Rustom Mody <rustompmody@gmail.com> - 2016-01-13 08:33 -0800
      Re: When I need classes? Rick Johnson <rantingrickjohnson@gmail.com> - 2016-01-14 10:00 -0800

csiph-web