python - Subclass' __getitem__ returns unexpected slice -


i have created class subclasses configparser.safeconfigparser implement sectionless config files. problem i'm having unexpected slice type compared how expect __getitem__ respond:

import configparser  class foo(configparser.safeconfigparser):     def __getitem__(self, option):         return option  class bar(object):     def __getitem__(self,option):         return option  = foo() b = bar() print a[:] print b[:] 

its reply puzzles me, get:

slice(0, 2147483647, none) slice(none, none, none) 

i have expected (none, none, none) in both cases. can guess behavior familiar--if example working simple list() slicing action--but makes hard determine user's intent via if option.start none, fails in former case.

what part of safeconfigparser changing behavior , can receive (none, none, none) instead of (0, sys.maxint, none)

safeconfigparser old-style class, , therefore foo. bar new-style class (derived object).

>>> type(configparser.safeconfigparser) <type 'classobj'>  >>> type(foo) <type 'classobj'>  >>> type(bar) <type 'type'> 

old-style classes have number of differences new-style ones; clearly, 1 of them, presumably backward compatibility (i.e. because that's how slicing used behave). has nothing safeconfigparser per se, can see here:

class baz:    # old-style class in python 2.x x >= 2     def __getitem__(self, option):         return option  c = baz() print c[:]    # slice(0, 2147483647, none) 

to around guess try update configparser use new-style classes. may reasonably easy; diff python 3's configparser (which not use old-style clasess, because there's no such thing in python 3) might helpful.


Comments