Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > user variables

Thread Tools

user variables

Posts: n/a
Sorry this must be really trivial, but I am new to Python...
suppose I defined

is there a command like


which would show



I tried globals(), locals() and vars(), but they all mix my user-defined
variables with system ones... clues? ideas?

thanks in advance,


Reply With Quote
John Hunter
Posts: n/a
>>>>> "user" == user <(E-Mail Removed)> writes:
user> I tried globals(), locals() and vars(), but they all mix my
user> user-defined variables with system ones... clues? ideas?

These are the right functions to be thinking about. If I may be so
bold, perhaps you are not asking the right question. What is it you
need to do? You say you want to get the user defined variables. To
what end? Perhaps if you describe what it is you need to do, not how
you plan to do it, someone can offer an insightful solution.

Barring that, would it be helpful to remove the system/module vars by
first saving the keys of locals before user input, eg

from os import * # import a bunch of non user-defined names for testing

def diffkeys(k1, k2):
"return the keys in d1 that are not in d2"
k2d = dict( [(k,1) for k in k2] )
return [k for k in k1 if not k2d.has_key(k)]

def items_for_keys(keys, d):
"return a list of (k,v) pairs for given keys in dict d"
seen = dict( [(k,1) for k in keys] )
return [ (k,v) for k,v in d.items() if seen.has_key(k)]

base = locals().keys()
#now come the user vars
x = 1
y = 2
new = locals().keys()
new.remove('base') #a user var we aren't interested in
print items_for_keys(diffkeys(new, base), locals())


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? Java 10 06-06-2011 06:43 AM
Put variables into member variables or function variables? C++ 9 03-23-2008 04:03 PM
Session Variables and Static Variables ASP .Net 1 03-26-2006 11:05 AM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM