-1

I'm trying to loop through a list of variables but I'm unable to access the variable when I append it with self

var_list = [account, user, authenticator, database, schema, warehouse, role]

for var in var_list:
    if var == None:
        var = self.var  # <---- the .var here is not taking the var from var_list
    else: 
        var    

Any ideas?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
mikelowry
  • 1,307
  • 4
  • 21
  • 43
  • 1
    What is `self` in this case? Why do you think that `self.var` should take values from `var_list`? What is the lone `var` in the `else` branch meant to do? – 0x5453 Oct 18 '21 at 18:36
  • https://www.oreilly.com/library/view/python-in-a/0596001886/ch04s03.html – Kaia Oct 18 '21 at 18:39
  • @0x5453 good question, This loop resides within a class's method. So the self is just the variables set in the __init__ function. – mikelowry Oct 18 '21 at 18:40
  • 1
    Does this answer your question? [How to access object attribute given string corresponding to name of that attribute](https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib) – Pranav Hosangadi Oct 18 '21 at 18:42
  • Are you trying to automate `account = self.account; user = self.user; # etc`? – chepner Oct 18 '21 at 18:42
  • @chepner if the variables are not inputted into a method of a class, use the variables that were used in the `__init__` function of the class. – mikelowry Oct 18 '21 at 18:44

3 Answers3

3

Your question isn't very clear, but it looks like you want to get, in order

  • self.account
  • self.user
  • self.authenticator
  • etc

You can do this using the getattr function:

var_list = ["account", "user", "authenticator", "database", "schema", "warehouse", "role"]

for var in var_list:
        var_value = getattr(self, var)
RoadieRich
  • 6,330
  • 3
  • 35
  • 52
1

There's no good way to update the value of a variable given its name as a string. I would just bite the bullet and write a series of if statements.

if account is None:
    account = self.account

if user is None:
    user = self.user

# etc

Note that I do not consider using exec to execute a dynamically constructed assignment statement a good alternative.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

RoadieRich is correct, though you should be careful using getattr/setattr too much when you can avoid it. (For instance, if your user could get control of var_list, they could get access to any attributes they wanted.)

If you just want to access the values of these attributes:

var_list = [self.account, self.user, self.authenticator, 
            self.database, self.schema, self.warehouse, self.role]

For a simple look at why this happens: Intuitively, python has a mapping of variable names to values. (many mappings, in reality, but let's ignore that for simplicity. You can read this post for a bit more info). That mapping might look like:

account =>> 1234
user =>> User class @ 0x0340 [A reference to a User class at some random place in memory]
...

When you run var_list = [account, user], it does something like

account =>> 1234
user =>> User class @ 0x0340
var_list =>> List class @ 0x0350 <list_0>
list_0<element 0> =>> 1234
list_0<element 1> =>> User class @ 0x0340
...

See that the elements of the list only know their values, not what they were called.

If your mapping instead looks like:

self =>> YourClass @ 0x0340 <your_class_0>
your_class_0<account> =>> 1234
your_class_0<user> =>> User class @ 0x0350
...

Then trying to do var_list = [account, user] will cause an error, because there isn't anything called account in the mapping table.

Kaia
  • 862
  • 5
  • 21