Jan-20-2018, 04:55 PM
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:
this code line executed directly in to the interactive mode of the python3 interpreter ( of some ide ) works perfectly:
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:
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:
- done
6. declare some variable:
- done
7. call the 'u'- function by:
- done
8. the output - an empty list:
instead of
how should i change the function 'u' so it can print just user's declared variables?
thanks in advance
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:
1 |
[val for val in dir () if val.strip( '__' ) = = val] |
1 2 |
>>> [val for val in dir () if val.strip( '__' ) = = val] [ 'a' ] |
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:
1 2 3 |
def u (): a = [val for val in dir () if val.strip( '__' ) = = val] return a |
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:
1 |
>>> import ud |
6. declare some variable:
1 |
a = 'ggfhgfjhh' |
7. call the 'u'- function by:
1 |
>>> ud.u() |
8. the output - an empty list:
1 |
[ ] |
1 |
[ 'a' ] |
thanks in advance