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

Minimize method calls – Python

I have this method get_info when I pass a key and it gives me a value of it.

name = self.get_info("name_key")
age = self.get_info("age_key")
gender = self.get_info("gender_key")
hometown = self.get_info("hometown_key")
state = self.get_info("state_key")

I need to extract name, age, gender, hometown, state.

Is there any way I can get the values in a minimized way (reduce duplicate lines)?

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

I need to call the method again and again for each value.

Thanks for the help.

>Solution :

You can use a loop along with spread assignment.

name, age, gender, hometown, state = [self.get_info(x) for x in ('name_key', 'age_key', 'gender_key', 'hometown_key', 'state_key')]
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