create global variables?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alistair King

    #1

    create global variables?

    Hi,

    is there a simple way of creating global variables within a function?

    ive tried simply adding the variables in:

    def function(atom, Xaa, Xab):
    Xaa = onefunction(ato m)
    Xab = anotherfunction (atom)

    if i can give something like:

    function('C') #where atom = 'C' but not necessarly include Xaa or Xab

    i would like to recieve:

    Caa = a float
    Cab = another float

    ive tried predefining Xaa and Xab before the function but they are
    global values and wont change within my function. Is there a simple way
    round this, even if i call the function with the variables ('C', Caa, Cab)?
    ............... ............... ............... ............... ............... ............... ............... ............... .......

    some actual code:

    # sample dictionaries
    DS1v = {'C': 6}
    pt = {'C': 12.0107}

    def monoVarcalc(ato m):
    a = atom + 'aa'
    Xaa = a.strip('\'')
    m = atom + 'ma'
    Xma = m.strip('\'')
    Xaa = DS1v.get(atom)
    Xma = pt.get(atom)
    print Xma
    print Xaa

    monoVarcalc('C' )

    print Caa
    print Cma
    ............... ............... ............... ............... ............... ............... ............... ............... .......
    it seems to work but again i can only print the values of Xma and Xaa

    ?

    Alistair

    --
    Dr. Alistair King
    Research Chemist,
    Laboratory of Organic Chemistry,
    Department of Chemistry,
    Faculty of Science
    P.O. Box 55 (A.I. Virtasen aukio 1)
    FIN-00014 University of Helsinki
    Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
    Fax +358 9 191 50366

  • robert

    #2
    Re: create global variables?

    Alistair King wrote:
    Hi,
    >
    is there a simple way of creating global variables within a function?
    >
    #module global:

    def f(atom):
    global a
    a=1
    globals()[atom+'var']=2

    def f():
    a=b=1
    globals().updat e(locals())

    _global=sys.mod ules[__name__]

    def f(atom):
    _global.a = 1
    setattr(_global ,atom+'var', 2)

    # all/app global

    import myglobals

    def f(atom):
    myglobals.a=1
    setattr(mygloba ls....)


    your example application seems to point towards 'odd' use of these techniques.


    -robert

    Comment

    • Wojciech Mu³a

      #3
      Re: create global variables?

      Alistair King wrote:
      is there a simple way of creating global variables within a function?
      def foo(name):
      globals()[name] = "xxx"
      globals()[name + 'aa'] = "yyy"
      globals()[name + 'ab'] = "zzz"

      Comment

      • J. Clifford Dyer

        #4
        Re: create global variables?

        Alistair King wrote:
        Hi,
        >
        is there a simple way of creating global variables within a function?
        >
        ive tried simply adding the variables in:
        >
        def function(atom, Xaa, Xab):
        Xaa = onefunction(ato m)
        Xab = anotherfunction (atom)
        >
        if i can give something like:
        >
        function('C') #where atom = 'C' but not necessarly include Xaa or Xab
        >
        i would like to recieve:
        >
        Caa = a float
        Cab = another float
        >
        ive tried predefining Xaa and Xab before the function but they are
        global values and wont change within my function. Is there a simple way
        round this, even if i call the function with the variables ('C', Caa, Cab)?
        ............... ............... ............... ............... ............... ............... ............... ............... .......
        >
        some actual code:
        >
        # sample dictionaries
        DS1v = {'C': 6}
        pt = {'C': 12.0107}
        >
        def monoVarcalc(ato m):
        a = atom + 'aa'
        Xaa = a.strip('\'')
        m = atom + 'ma'
        Xma = m.strip('\'')
        Xaa = DS1v.get(atom)
        Xma = pt.get(atom)
        print Xma
        print Xaa
        >
        monoVarcalc('C' )
        >
        print Caa
        print Cma
        ............... ............... ............... ............... ............... ............... ............... ............... .......
        it seems to work but again i can only print the values of Xma and Xaa
        >
        ?
        >
        Alistair
        >
        I suspect you are misusing the concept of a function. In most basic
        cases, and I suspect your case applies just as well as most, a function
        should take arguments and return results, with no other communication
        between the calling code and the function itself. When you are inside
        your function don't worry about the names of the variables outside. I'm
        not sure exactly where your floats are coming from, but try something
        like this:
        >>def monoVarCalc(rel evant_data):
        .... float1 = relevant_data * 42.0
        .... float2 = relevant_data / 23.0
        .... return float1, float2
        >>C = 2001
        >>Caa, Cab = monoVarCalc(C)
        >>Caa
        84042.0
        >>Cab
        87.0

        Notice that you don't need to use the variable C (or much less the
        string "C", inside monoVarCalc at all. It gets bound to the name
        relevant_data instead.

        Also, if you are going to have a lot of these little results lying
        around, (Cab, Cac ... Czy, Czz), you might consider making them a list
        or a dictionary instead. I won't tell you how to do that, though. The
        online tutorial has plenty of information on that.

        Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...



        Cheers,
        Cliff

        Comment

        • Fredrik Lundh

          #5
          Re: create global variables?

          Wojciech Muła wrote:
          >is there a simple way of creating global variables within a function?
          >
          def foo(name):
          globals()[name] = "xxx"
          globals()[name + 'aa'] = "yyy"
          globals()[name + 'ab'] = "zzz"
          that kind of coding is punishable by law in some jurisdictions.

          </F>

          Comment

          • Steve Holden

            #6
            Re: create global variables?

            Wojciech Muła wrote:
            Alistair King wrote:
            >
            >>is there a simple way of creating global variables within a function?
            >
            >
            def foo(name):
            globals()[name] = "xxx"
            globals()[name + 'aa'] = "yyy"
            globals()[name + 'ab'] = "zzz"
            Please note that this is a terrible programming practice. Anyone who
            really thinks they need to do this should be thinking hard about whether
            they are using Python correctly (though tere are occasionaly
            requirements where the feature can legitimately be used).

            It would be *much* easier and *much* better programming practice to
            modify your code so the function returns a two-element tuple, which you
            then assign to two variables in an unpacking assignment.

            regards
            Steve
            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Skype: holdenweb http://holdenweb.blogspot.com
            Recent Ramblings http://del.icio.us/steve.holden

            Comment

            Working...