<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br><br>For me, the "()" look like artificial, not necessary. I would prefer just to type "a.list_1stpart" , a property.<br>
<br><br><br>-- <br><br></blockquote></div>Others have explained their preference for using get methods for accessing internal data structures, <div>However it does look like you have specifically mentioned a preference for attribute like access:</div>
<div><br></div><div>e = ExampleList([1,2,3,4], 2)</div><div>>>> e.firstpart</div><div>[1,2]</div><div><br></div><div>rather than</div><div>>>> e.firstpart()</div><div>[1,2]</div><div><br></div><div>We can implement this using properties, and I will refer you to some of the documentation <a href="http://docs.python.org/library/functions.html#property">http://docs.python.org/library/functions.html#property</a></div>
<div><br></div><div>Here is just one way that you could simply implement a property in your case:</div><div><br></div><div>class ExampleList(object):</div><div> """Note that this is a new style class."""</div>
<div> def __init__(self, sequence, position):</div><div> self._sequence = sequence</div><div> self._position = position</div><div> @property</div><div> def firstpart(self):</div><div> """This method will be called on inst.firstpart rather than inst.firstpart()."""</div>
<div> return self._sequence[:self._position]</div><div><br></div><div>Here I have used property as a decorator (described in the link), now you can get your</div><div>firstpart through attribute access (not that you cannot 'set' to it):</div>
<div> </div><div>e.firstpart</div><div><br></div><div>Cheers,</div><div><br></div>