user variables

Discussion in 'Python' started by user@domain.invalid, Jun 24, 2003.

  1. Guest

    Sorry this must be really trivial, but I am new to Python...
    suppose I defined
    a=5
    b=7
    c=9

    is there a command like

    usr_vars()

    which would show

    a=5
    b=7
    c=9

    ????

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

    thanks in advance,

    Pier
     
    , Jun 24, 2003
    #1
    1. Advertising

  2. John Hunter Guest

    >>>>> "user" == user <> 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())


    JDH
     
    John Hunter, Jun 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,398
    avnrao
    May 7, 2004
  2. Replies:
    1
    Views:
    964
  3. dwok
    Replies:
    7
    Views:
    729
    Hal Rosser
    Mar 4, 2005
  4. Replies:
    10
    Views:
    36,021
    jporter892
    Jun 6, 2011
  5. Replies:
    9
    Views:
    1,008
Loading...

Share This Page