[Simple] Accessing a property with Python variables

Asked 2 years ago, Updated 2 years ago, 34 views

I have a question, but I can't find it well even if I search for it, so I'm asking

If the class foo has an attribute called bar,

tmp = foo()

tmp.bar

You can approach it like this.

If so

a = when 'bar'

Can I access the property of foo.bar with a? The question is a bit difficult, but I'd appreciate it if you could answer it.

python python3

2022-09-22 18:39

1 Answers

Many modern languages and environments are applying under the name of RTTI, reflection, etc.

class Abc:
    def __init__(self):
        self.a = 5

abc = Abc()
getattr(abc, 'a')

5


2022-09-22 18:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.