0

im calling them keyed arrays because if i knew what they were called i could find the answer myself =-)

ok, for example:

parser = OptionParser(conflict_handler="resolve")
parser.add_option("-x", dest="var_x",  help="")
parser.add_option("-y", dest="var_y",  help="")


(options, args) = parser.parse_args()

generates an option object that can be used like this:

foobar = options.var_x

what are these called and where would i find some documentation on how to create and use them?

0

3 Answers 3

2

One class that does something very similar is namedtuple:

In [1]: from collections import namedtuple

In [2]: Point = namedtuple('Point', ['x', 'y'])

In [4]: p = Point(1, 2)

In [5]: p.x
Out[5]: 1

In [6]: p.y
Out[6]: 2
Sign up to request clarification or add additional context in comments.

Comments

1

One poss is to wrap a dictionary in an object see below for class definition:

class Struct:
def __init__(self, **entries): 
    self.__dict__.update(entries)

Then just use a dictionary in constructor like so:

adictionary = {'dest':'ination', 'bla':2}
options = Struct(**adictionary)
options.dest
options.bla

options.dest will return 'ination'

options.bla would return 2

Comments

1

If you do help(options) at the interactive terminal, you'll see this is an optparse.Values instance. It's not intended for making your own things, really.

Using attribute access for key–value pairs is usually silly. Much of the time people who insist on it should really just be using a dict.

The main built-in way to do something along these lines is collections.namedtuple.

Comments

Your Answer

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.