[Tutor] designing a class

Kent Johnson kent37 at tds.net
Fri Jan 27 17:49:00 CET 2006


Christopher Spears wrote:
>>class MyList:
>>   def __init__(self, aList=None):
>>     if aList is None:
>>       self._list = []
>>     else:
>>       self._list = aList[:]
>>
> 
> 
> This code certainly looks like it will do the trick. 
> I'm just not sure what the _ in front of list (i.e.
> _list) denotes.

It's a convention that indicates a private attribute. Think of it as 
telling clients, "This is for my private use, it may change at any time."

In this case it was also a way to avoid using 'list' as the name of the 
attribute. 'list' is the name of a built-in - it shouldn't be used as a 
variable name because it will shadow the built-in. In the case of an 
attribute it doesn't really matter but I have a strong habit of not 
using 'list' as a name.

Kent



More information about the Tutor mailing list