[Tutor] stateless Vs operation with state

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jun 18 05:14:44 EDT 2021


On 18/06/2021 07:13, Manprit Singh wrote:
> An example of operation with state :
> balance of 2000  and he deposits  4000 . then his balance becomes 6000 . so
> this present balance is dependent on the previous balance . According to me
> this problem is a problem with state and for such kind of problems OOPS or
> classes  are  the solution .

They might be. But you could also use a global variable, or in a real
world problem, a database. In which case functions will work just as well.

In fact in Python, if it is just a single value you are tracking it
could even be a generator function. That wouldn't apply with bank
balances since there will be separate balances per customer, but
in a control system it might be the case.

However, for more complex systems, OOP has become the standard
option for managing stateful entities. But database plus
functions are still a valid option.


> Example of stateless:
> 
> Stateless problems are those problems, in which i can get the answer of the
> problem based on the present scenario only , and that answer  is not going
> to alter or change the  input in any way . Functions are best  for these

Here I agree, and in fact not just functions but the whole
functional programming paradigm is directly suited to stateless
applications.

You can use OOP in stateless apps but there is little advantage.
However, there are very, very, few stateless applications in the
real world!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list