Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- options = ??????
- options.VERBOSE = True
- options.IGNORE_WARNINGS = False
- # Then, elsewhere in the code...
- if options.VERBOSE:
- ...
- options = object()
- options.VERBOSE = True
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- AttributeError: 'object' object has no attribute 'VERBOSE'
- class options(object):
- VERBOSE = True
- IGNORE_WARNINGS = True
- if options.VERBOSE:
- # ...
- VERBOSE = True
- IGNORE_WARNINGS = True
- import options
- if options.VERBOSE:
- # ...
- import collections
- opt=collections.namedtuple('options','VERBOSE IGNORE_WARNINGS')
- myoptions=opt(True, False)
- >>> myoptions
- options(VERBOSE=True, IGNORE_WARNINGS=False)
- >>> myoptions.VERBOSE
- True
- class options(object):
- pass
- myoptions=options()
- myoptions.VERBOSE=True
- myoptions.IGNORE_WARNINGS=False
- >>> myoptions.IGNORE_WARNINGS,myoptions.VERBOSE
- (False, True)
- >>> x = lambda: 0 # any function will do
- >>> x.foo = 'bar'
- >>> x.bar = 0
- >>> x.xyzzy = x
- >>> x.foo
- 'bar'
- >>> x.bar
- 0
- >>> x.xyzzy
- <function <lambda> at 0x6cf30>
- >>> dir(x)
- ['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
- '__get__', '__getattribute__', '__hash__', '__init__',
- '__module__', '__name__', '__new__', '__reduce__',
- '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'foo',
- 'func_closure', 'func_code', 'func_defaults', 'func_dict',
- 'func_doc', 'func_globals', 'func_name', 'xyzzy']
- from optparse import OptionParser
- [...]
- parser = OptionParser()
- parser.add_option("-f", "--file", dest="filename",
- help="write report to FILE", metavar="FILE")
- parser.add_option("-q", "--quiet",
- action="store_false", dest="verbose", default=True,
- help="don't print status messages to stdout")
- (options, args) = parser.parse_args()
- file = options.filename
- if options.quiet == True:
- [...]
- options = type('Options', (object,), { 'VERBOSE': True })()
- options.IGNORE_WARNINGS = False
- Options = type('Options', (object,), {})
- options = Options()
- options.VERBOSE = True
- options.IGNORE_WARNINGS = False
- class attrdict(dict):
- def __init__(self, *args, **kwargs):
- dict.__init__(self, *args, **kwargs)
- self.__dict__ = self
- >>> ad = attrdict({'foo': 100, 'bar': 200})
- >>> ad.foo
- 100
- >>> ad.bar
- 200
- >>> ad.baz = 'hello'
- >>> ad.baz
- 'hello'
- >>> ad
- {'baz': 'hello', 'foo': 100, 'bar': 200}
- >>> isinstance(ad, dict)
- True
- class attrdict2(object):
- def __init__(self, *args, **kwargs):
- self.__dict__.update(*args, **kwargs)
- >>> ad = attrdict2(foo = 100, bar = 200)
- class attrdict3(object):
- pass
- ad = attrdict3()
- ad.foo = 100
- ad.bar = 200
- class options(object):
- VERBOSE = True
- IGNORE_WARNINGS = False
- options.VERBOSE = False
- if options.VERBOSE:
- ...
- class Struct:
- def __init__(self, **entries):
- self.__dict__.update(entries)
- john = Struct(name='john doe', salary=34000)
- print john.salary
Add Comment
Please, Sign In to add comment