Lets say I have the code:
class NoneDict(dict):
def __getitem__(self, name):
try:
return super(NoneDict, self).__getitem__(name)
except:
return None
I want people to be able to create a NoneDict object without writing all this code out. I tried including it in a module and then typed:
import nonedict
foo = NoneDict()
But it didn't work. How can I make it so someone can import the module and then be able to create a nonedict without typing out all the code?
Karl Knechtel
61.5k14 gold badges134 silver badges194 bronze badges
asked Apr 5, 2012 at 2:43
Billjk
10.7k23 gold badges56 silver badges74 bronze badges
2 Answers 2
import nonedict
foo = nonedict.NoneDict()
or
from nonedict import NoneDict
foo = NoneDict()
or (thanks @Joel Cornett)
from nonedict import NoneDict as nd
foo = nd()
answered Apr 5, 2012 at 2:46
dkamins
22.1k7 gold badges58 silver badges62 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Joel Cornett
You could also do
from nonedict import NoneDict as nd
answered Apr 5, 2012 at 2:44
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Comments
lang-py
from nonedict import NoneDict?