Classes Inside Modules

Go To StackoverFlow.com

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?

2012-04-05 02:43
by Billjk
Did you try from nonedict import NoneDict - Praveen Gollakota 2012-04-05 02:44


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()
2012-04-05 02:46
by dkamins
You could also do from nonedict import NoneDict as ndJoel Cornett 2012-04-05 03:39


2

Ads