0

I want to automatically define variables using a dynamic string, similar to ${$a} in PHP.

As I do this in a plain script I do not have objects and therefore can not apply setattr or am I wrong?

How do I dynamically define variables?

1
  • use a dictionary Commented Oct 17, 2018 at 10:38

2 Answers 2

0

You can store the variables you want in a dictionary. That's basically what php does, only in Python you can specify which dictionary to use and how to handle it.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can modify local or global dictionary:

>>> locals()['xxx'] = 'Hi'
>>> xxx
'Hi'
>>> globals()['yyy'] = 'Hello'
>>> yyy
'Hello'

1 Comment

Don't. Use a dedicated dict instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.