Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign parameter value to self?

Is it possible to assign a function parameter into self directly?

The code below is an example of what I’d like to achieve.

class SomeClass:
    def some_function(self, key)
        self.<key> = 1

I managed something similar for a param keyword using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

**{key: val}

But can’t work out how to do something similar for self

>Solution :

You can use setattr and pass the key as a string

class SomeClass:
    def some_function(self, key, value):
        setattr(self, key, value)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading