contained in
it:  1

    
    
    >>> b = Bank()                                                                                                                                                                                                                                                                                                                                     
    >>> b[7]                                                                                                                                                                                                                                                                                                                                           
    7
    >>> b[-3:0]                                                                                                                                                                                                                                                                                                                                      
    -6
    >>> b[0:7:2]                                                                                                                                                                                                                                                                                                                                      
    12
    >>> bank[::]
    0 
    >>> bank['':5:{}]
    10
    

As the last example demonstrates, slices can contain just about any value, not
just integers and ` None ` , thus the ` Bank ` class must check for these
cases and use defaults if needed.

Just a word of caution – if sub-classing built-in types in Python 2, and want
to implement custom slicing behavior, you need to override the deprecated `
__getslice__() ` method ( [ documentation
](https://docs.python.org/2/reference/datamodel.html#object.__getslice__) ).

* * *

  1. Not saying that this is actually the best way to run a bank in real life, nor that (ab)using slices like this will make you popular with people using your sliceable class…  ↩ 

###  Share this:

  * [ Twitter  ](https://blog.peterlamut.com/2017/12/30/giving-python-slices-a-name/?share=twitter "Click to share on Twitter")
  * [ Facebook  ](https://blog.peterlamut.com/2017/12/30/giving-python-slices-a-name/?share=facebook "Click to share on Facebook")
  * 

Like  Loading...

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=49&d=identicon&r=G)
Author  [ Peter Lamut ](https://blog.peterlamut.com/author/peterlamut/) Posted
on  [ December 30, 2017  ](https://blog.peterlamut.com/2017/12/30/giving-
python-slices-a-name/) Tags  [ Python
](https://blog.peterlamut.com/tag/python/) , [ Sequences
](https://blog.peterlamut.com/tag/sequences/) , [ Slices
](https://blog.peterlamut.com/tag/slices/) [ Leave a comment  on Giving Python
slices a name  ](https://blog.peterlamut.com/2017/12/30/giving-python-slices-
a-name/#respond)

##  About the Author

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=96&d=identicon&r=G)  
I am an experienced software engineer, primarily using Python and JavaScript
in my day-to-day work, putting a strong emphasis on writing clean and
maintainable code.

I use Linux, like to crack jokes, and enjoy attending developer meetups and
conferences like EuroPython.

I do not own any pets or hair care products.

Search for:  Search

##  Tags

  * [ Attribute lookup ](https://blog.peterlamut.com/tag/attribute-lookup/)
  * [ ChainMap ](https://blog.peterlamut.com/tag/chainmap/)
  * [ Classmethod ](https://blog.peterlamut.com/tag/classmethod/)
  * [ CPython internals ](https://blog.peterlamut.com/tag/cpython-internals/)
  * [ Debugging ](https://blog.peterlamut.com/tag/debugging/)
  * [ Decorators ](https://blog.peterlamut.com/tag/decorators/)
  * [ Descriptors ](https://blog.peterlamut.com/tag/descriptor