Kin
Kin

Reputation: 4596

Is it possible to extract values from namespace to class?

For example values from args i can extract like this

globals().update(vars(args))

Is it possible automatically create same properties fro class? Something like this

class SomeClass():
    def __init__(self, args):
        globals().update(vars(args))
        exit(self.location)

Upvotes: 0

Views: 40

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122582

I think you meant you want to update the attributes on the class; you can do so with:

class SomeClass():
    def __init__(self, args):
        vars(self).update(vars(args))

Demo:

>>> class Foo:
...     def __init__(self):
...         self.bar = 'baz'
... 
>>> result = SomeClass(Foo())
>>> result.bar
'baz'

Upvotes: 1

Related Questions