bofjas
bofjas

Reputation: 1206

Simple way to pass attributes as parameters without defining a class

Say I have a function:

def test(ab):
    print ab.a
    print ab.b

How can I easily pass arguments to this function without defining a class? I'm looking for something along the lines of:

test({4,5})

or

test(ab.a = 5, ab.b = 6)

Is this at all possible? Changing the function 'test' is not an option.

Upvotes: 0

Views: 108

Answers (1)

user647772
user647772

Reputation:

Use collections.namedtuple:

import collections

ab = collections.namedtuple("AB", "a b")(a=4, b=5)
test(ab)

Upvotes: 6

Related Questions