Python Forum
is it pythonic to add a name 'attribute' to an existing variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it pythonic to add a name 'attribute' to an existing variable?
#4
A variable is just a name. Think of it as a key in a namespace dictionary. The only thing you can use a variable for is getting or setting the object associated with the variable name. Even if you could somehow attach a name to a variable, it would do you no good. Your list vars does not contain any variables. It contains objects. vars has no association with the variable run1. vars[0] and run1 just happen to reference the same object, and that can change at any time. Variables do not provide you with a way to do what you want.

You'll get an error if you try to set the "name" attribute of a string or a list. These classes prohibit you from adding attributes (likely something in the __setattr__()). You probably cannot do what you want by attaching the name to the object.

You could create a new object that includes the name and the object.
vars = [(var, f'name{i}) for i, var in enumerate(vars)]
Since it appears the filename is based on the array index, why not generate the names when you need them?
Reply


Messages In This Thread
RE: is it pythonic to add a name 'attribute' to an existing variable? - by deanhystad - Oct-04-2021, 08:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create new variable dependent on two existing variables JoeOpdenaker 6 3,121 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,338 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  which is "better" (or more Pythonic)? Skaperen 2 2,086 Feb-01-2020, 03:10 PM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 7 3,273 Feb-01-2020, 03:51 AM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 8 3,415 Nov-16-2019, 06:46 PM
Last Post: Skaperen
  which is more Pythonic? Skaperen 5 2,906 Jul-16-2019, 01:00 AM
Last Post: Skaperen
  Function Attribute / Global Variable Confusion digitalmatic7 1 5,413 Feb-13-2018, 09:54 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020