Python Forum
list of user's variables in the interpreter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of user's variables in the interpreter
#1
hi,
i would like to ask how could i put a line of code that prints just user's declared variables in a function so it can be reused in a different scripts?

the code line is:
 [val for val in dir() if val.strip('__') == val] 
this code line executed directly in to the interactive mode of the python3 interpreter ( of some ide ) works perfectly:
 >>> [val for val in dir() if val.strip('__') == val]
['a'] 
the problems begin when the following procedure has been executed in some ide.

1. put the code line '[val for val in dir() if val.strip('__') == val]' in a separate function called 'u' - done
the 'u'-function looks like this:
def u ():
		a = [val for val in dir() if val.strip('__') == val] 
		return a   
2. put the function 'u' in a python-file called 'ud.py' - done

3. open the 'ud.py' in the ide - done

4. open python3 interpreter of the ide in interactive mode - done

5. import 'ud.py' like a module 'ud' by:
 >>> import ud  
- done

6. declare some variable:
a = 'ggfhgfjhh'  
- done

7. call the 'u'- function by:
>>> ud.u()  
- done

8. the output - an empty list:
 [ ]  
instead of
 ['a']  
how should i change the function 'u' so it can print just user's declared variables?

thanks in advance
Reply
#2
As per the documentation,
Quote:dir() returns the list of names in the current local scope
what you are trying to do is get the same listing for different scopes.
Can't be done with this command.
Reply
#3
If you want to access the parameter names of a function or the local variables defined in this function, the correct way is to use module inspect and attributes of code objects, for example:
>>> import inspect
>>> def spam(a, b, c='foo'):
...     bar = a + c
...     qux = [x for x in b if x.is_baz()]
... 
>>> spam.__code__.co_varnames
('a', 'b', 'c', 'bar', 'qux')
>>> s = inspect.signature(spam)
>>> s.parameters
mappingproxy(OrderedDict([('a', <Parameter "a">), ('b', <Parameter "b">), ('c', <Parameter "c='foo'">)]))
Reply
#4
one possible solution could be to use the line [val for val in dir() if val.strip('__') == val] directly in to the
interactive mode of the interpreter just by coping and pasting it in to the interpreter or if the interpreter has
a history on the '->' button to scroll down and up with it.
Reply
#5
If you only want the names in the main namespace you can use
# file ud.py
def u():
    import __main__
    return [w for w in dir(__main__) if w.strip('__') == w]
Then
>>> spam = 'eggs'
>>> ham = 3
>>> import ud
>>> ud.u()
['ham', 'spam', 'ud']
Reply
#6
very nice and elegant solution! thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 499 May-13-2023, 03:30 AM
Last Post: knight2000
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  User-defined function to reset variables? Mark17 3 1,588 May-25-2022, 07:22 PM
Last Post: Gribouillis
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Setting permanent user variables in Windows coder420 2 1,381 Jan-04-2022, 10:42 AM
Last Post: Larz60+
  Looking for a way to loop until user enters from a list? PythonW19 7 3,265 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  User input/picking from a list AnunnakiKungFu 2 2,282 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
Question Reset list if user regrets Gilush 1 2,043 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Converting list to variables Palves 1 1,730 Sep-18-2020, 05:43 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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