Question

How would I go about making reference to an element from a list inside that list? For example,

settings = ["Exposure", "0", random_time(settings[0])]

Where the third element makes reference to the first. I could verbosely state "Exposure" but I am trying to set it up so that even if the first element is changed the third changes with it.

Edit: I think maybe my question wasn't clear enough. There will be more than one setting each using the generic function "random_time", hence the need to pass the keyword of the setting. The reference to the first element is so I only have to make modifications to the code in one place. This value will not change once the script is running.

I will try and use a list of keywords that the settings list makes reference to.

Was it helpful?

Solution

The right-hand expression is evaluated first, so when you evaluate

["Exposure", "0", random_time(settings[0])]

the variable settings is not defined yet.

A little example:

a = 1 + 2

First 1 + 2 is evaluated and the result is 3, after it's evaluated, then the assignment is done:

a = 3

One way you could handle this is storing the "changing" string to a variable:

var1 = "Exposure"
settings = [var1 , "0", random_time(var1)]

this will work in the list definition, but if, after declaring the list settings, you change var1, it won't change its third element. If you want this to happen, you can try implementing a class Settings, which will be a lot more flexible.

OTHER TIPS

AFAIK you can't. This is common to most programming languages because when you're running your function there the item hasn't been completely created yet.

You can't directly.

You could have both refer to something else, though, and use an attribute of that.

class SettingObj:
    name = "Exposure"

settings = [SettingObj, "0", random_time(SettingObj)]

Now, change the way you work with your settings list so that you look for your name attribute for 1st and 3rd items on the list.

As others have told you, the syntax you've chosen will try to reference settings before it is created, and therefore it will not work (unless settings already exists because another object was assigned to it on a previous line).

More importantly, in Python, assigning a string to two places will not make it so that changing it in one place will change it in the other. This applies to all forms of binding, including variable names, lists and object attributes.

Strings are immutable in Python -- they cannot be changed, only rebinded. And rebinding only affects a single name (or list position or etc.) at a time. This is different from, say, C, where two names can contain pointers that reference the same spot in memory, and you can edit that spot in memory and affect both places.

If you really need to do this, you can wrap the string in an object (custom class, presumably). You could even make the object's interface look like a string in all respects, except that it's not a string primitive but an object with an attribute (say contents) that's bound to a string. Then when you want to change the string, you rebind the object's attribute (that is, obj.contents or whatever). Since you are not reassigning the names bound to the object itself, but only a name inside the object, it will change in both places.

In this particular case you don't just have the same string in both places but you actually have a string in the first position but the result of a function performed on the string in the third position. So even if you use an object wrapper, it won't work the way you seem to want it to, because the function needs to be re-run every time.

There are ways to design your program so that this is not a problem, but without knowing more about your ultimate goal I can't say what they are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top