1

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
1
  • 1
    Did you try from nonedict import NoneDict ? Commented Apr 5, 2012 at 2:44

2 Answers 2

3
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
Sign up to request clarification or add additional context in comments.

1 Comment

You could also do from nonedict import NoneDict as nd
2
answered Apr 5, 2012 at 2:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.