________________________________ > From: Alan Gauld <alan.ga...@btinternet.com> >To: tutor@python.org >Sent: Friday, April 25, 2014 12:15 AM >Subject: Re: [Tutor] attributes vs globals (was Re: global list) > > >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..
Hi all, Sorry for the delayed reply, I was on holiday. Yes, Alan said what I meant with my original (somewhat vague) description. The snippet below further illustrates this (orignally I had only pseudo_global2 in mind, but the pseudo_global1 is another option). self.pseudo_global*'s value can be accessed everywhere, even though it is not an argument of the function or method. class Foo(object): pseudo_global1 = "I am all over the place, if I want to" def __init__(self): self.pseudo_global2 = "I am also all over the place, if I want to" print self.pseudo_global2 def some_method(self): print self.pseudo_global2 def some_function(): print self.pseudo_global2 some_function() print Foo.pseudo_global11 if __name__ == "__main__": Foo().some_method() regards, Albert-Jan _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor