qwertz
qwertz

Reputation: 459

How to call a value of an object in a list by using string

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?

Upvotes: 2

Views: 124

Answers (2)

mSkou
mSkou

Reputation: 94

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.

Upvotes: 0

SamProell
SamProell

Reputation: 381

using the special variable __dict__ should achieve what you want:

dots[whichCircle].__dict__[whichDir] = whichSpeed

Upvotes: 1

Related Questions