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


Groups > comp.lang.python > #101465

Re: When I need classes?

From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: When I need classes?
Date 2016-01-11 18:27 +1100
Message-ID <mailman.5.1452497235.13488.python-list@python.org> (permalink)
References <dcc532c3-c8ab-45f0-8fea-ef1fe8ad6af3@googlegroups.com>

Show all headers | View raw


On 10Jan2016 22:45, Arshpreet Singh <arsh840@gmail.com> wrote:
>On Sunday, 10 January 2016 20:33:20 UTC+5:30, Michael Torrie  wrote:
>> 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,
>
>I have a case in Flask-Oauth2 where one function returns Username, Email ID and Authorised Token.
>
>So I can make that function Global and access EMail,username and Authorised token from any other Script.

Note: you can access the _function_ from another script or module. When your 
programs do something like:

  from os.path import basename

they are doing exactly this.

>Or
>I can make it class?

On its own it doesn't mean much. Michael Torrie's criterion said "share state 
between functions"; that state is normally an instance of the class. So you 
have some value and a bunch of standard things you would do with that kind of 
value. That is the situation where a class is a natural thing to use: you make 
a class to represent the value, and each of the standard things you would do 
with one of those values is a class method.

In your situation above I would be inclined to make a class to represent the 
3-tuple you outline above: Username, Email ID and Authorised Token. So:

  from collections import namedtuple
  Authorisation = namedtuple('Authorisation', 'username email authorised_token')

now, where you would have obtained these as a tuple:

  # call your "in Flask-Oauth2 where one function returns Username..."
  authorisation = get_auth_info(...)

and then access authorisation[0] for the username and so forth, you can go:

  # fetch the 3 values and make an "Authorisation" from them
  authorisation = Authorisation(get_auth_info(...))

and the access authorisation.username, authorisation.email etc. This avoids 
knowing special index numbers (0, 1 and 2) which makes your code more readable 
and also makes it easy to pass around the authorisation for use.

Then, if you have things you routinely do with an "Authorisation" you can make 
methods for them. So that your code can say:

  authorisation.do_this(...)

and so forth.

>>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.
>
>Is it also true that Classes(OOP) help to optimise code(uses less memory) rather than just doing things with functions.

Not really? I would not expect using a class to inherently get you less memory 
use, just better and more consistent naming. There are some special situations 
where you can use a class to reduce your memory usage, but they are fairly 
dependent on what you're doing.

Cheers,
Cameron Simpson <cs@zip.com.au>

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