When I need classes?

Arshpreet Singh arsh840 at gmail.com
Mon Jan 11 01:45:25 EST 2016


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.

Or 
I can make it class?
  
>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.



More information about the Python-list mailing list