Python Forum
How to use a module as dict ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a module as dict ?
#1
is it possible to add dictionary property to module.  we can add dictionary property to classes with adding __getitem__ method. i hope we can do that same thing on modules.

example

import mymodule

print( mymodule["2 + 2"]  )

# output =>  '4'

I tried it but it not working

#mymodule.py

__dict__ = {"2 + 2": "4",  "python": 3.4, "os": "fedora"}

def __getitem__(key):
    return __dict__[key]
Reply


Messages In This Thread
How to use a module as dict ? - by harun2525 - May-15-2017, 04:58 PM
RE: How to use a module as dict ? - by micseydel - May-15-2017, 05:06 PM
RE: How to use a module as dict ? - by nilamo - May-15-2017, 05:24 PM
RE: How to use a module as dict ? - by snippsat - May-15-2017, 05:45 PM
RE: How to use a module as dict ? - by harun2525 - May-15-2017, 07:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 101,932 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020