[Tutor] cases with single if and an else clause

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 5 19:48:21 EDT 2021


On 06/10/2021 00:23, dn via Tutor wrote:

>> In OOP we tend to rely on using instance attributes to hold
>> state and its perfectly OK for methods to access that state

> If the object has its own (dict of) self, and there's no need to pass-in
> (or receive back) any values which are attributes - isn't this a new
> form of 'global danger' and how do I test, keep control ...

It can initially seem like that, except that the attributes are
encapsulated in an instance - an object. And in OOP all variables are -
or should be - objects, so you pass the objects around and changes to
the state are controlled by the object (via the methods). So the evils
of globals are greatly reduced.

For example, methods can only be reused in the context of an
instance so the dependant "external" state is guaranteed to exist.
And provided the objects are single threaded (or created thread safe)
only one  method can be changing the attributes at once. And the
class as a namespace avoids name collisions.

-- 
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