[Tutor] attributes vs globals (was Re: global list)

Alan Gauld alan.gauld at btinternet.com
Fri Apr 25 00:15:16 CEST 2014

On 24/04/14 21:48, Mark Lawrence wrote:
> On 24/04/2014 21:30, Albert-Jan Roskam wrote:
>> As a side not, I find that variables (attributes) defined in __init__
>> are also much like globals, but that's probably a different discussion.
> Would you please be kind enough to explain your logic.

I can't speak for Albert but I do tend to agree that class
(or instance) variables are a lot like globals. They are
slightly better controlled because they are unique to an
instance but they do result in code(methods) that are
dependant on side effects. You are changing data that
is neither passed into the method or returned by it.
So its not obvious which instance attributes are being
modified by which methods.

Now OOP theory says that with information hiding you
shouldn't care, so long as the external behaviour is
the correct, but in Python its  common to directly
access attributes. And if you inherit a class you
often do care about its internal state.

So instance variables carry many of the same negative
characteristics that globals do, albeit confined to a
single entity and a single, constrained, set of functions..

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list