When I need classes?

Cameron Simpson cs at zip.com.au
Mon Jan 11 02:27:10 EST 2016


On 10Jan2016 22:45, Arshpreet Singh <arsh840 at 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 at zip.com.au>



More information about the Python-list mailing list