Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Arup Rakshit
On 23/04/19 10:08 PM, Steven D'Aprano wrote: On Tue, Apr 23, 2019 at 08:27:15PM +0530, Arup Rakshit wrote: You probably want:     def __init__(self, list=None): if list is None:     list = [] self.list = list That is really a new thing to me. I didn't know. Why

Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Steven D'Aprano
On Tue, Apr 23, 2019 at 08:27:15PM +0530, Arup Rakshit wrote: > >You probably want: > > > >     def __init__(self, list=None): > > if list is None: > >     list = [] > > self.list = list > > That is really a new thing to me. I didn't know. Why list=None in the > param

Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Mats Wichmann
On 4/23/19 8:57 AM, Arup Rakshit wrote: > On 23/04/19 3:40 PM, Steven D'Aprano wrote: >> Watch out here, you have a mutable default value, that probably doesn't >> work the way you expect. The default value is created ONCE, and then >> shared, so if you do this: >> >> a = MyCustomList()  # Use the

Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Arup Rakshit
On 23/04/19 3:40 PM, Steven D'Aprano wrote: Watch out here, you have a mutable default value, that probably doesn't work the way you expect. The default value is created ONCE, and then shared, so if you do this: a = MyCustomList() # Use the default list. b = MyCustomList() # Shares the same de

Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Steven D'Aprano
On Tue, Apr 23, 2019 at 11:46:58AM +0530, Arup Rakshit wrote: > Hi, > > I wrote below 2 classes to explore how __getitem__(self,k) works in > conjuection with list subscriptions. Both code works. Now my questions > which way the community encourages more in Python: if isinstance(key, > slice):

Re: [Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Alan Gauld via Tutor
On 23/04/2019 07:16, Arup Rakshit wrote: > which way the community encourages more in Python: if isinstance(key, > slice): or if type(key) == slice: ? I think isinstance is usually preferred although I confess that I usually forget and use type()... But isinstance covers you for subclasses too.

[Tutor] Question on implmenting __getitem__ on custom classes

2019-04-23 Thread Arup Rakshit
Hi, I wrote below 2 classes to explore how __getitem__(self,k) works in conjuection with list subscriptions. Both code works. Now my questions which way the community encourages more in Python: if isinstance(key, slice): or if type(key) == slice: ? How should I implement this if I follow duck