2

I want to call a value of an object stored in a list:

List is: circles #storing 40 circle objects

The object is: a circle storing values #direction in x and y

Now i want to call circles[0].dirnx but dirnx is a string called whichDir

In Code:

whichCircle = 0
whichSpeed = 1
whichDir = 'dirnx' # dirnx = direction in x
dots[whichCircle].whichDir = whichSpeed

Of course python creates dots[whichCircle].whichDir and stores whichSpeed but i want to store in dots[whichCircle].dirnx

What is the proper way to do this?

2 Answers 2

1

using the special variable __dict__ should achieve what you want:

dots[whichCircle].__dict__[whichDir] = whichSpeed
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to find an item with a specific value in a list, you can do the following:

list_name.index('dirnx')

This will return the index of the position in the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.