global variables ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ruari mactaggart

    global variables ?

    how do I assign a value to a variable inside a function then use it in the
    main body ?

    i am trying to unpickle a dictionary in a function to use it in the program

    ruari


  • Alex Martelli

    #2
    Re: global variables ?

    ruari mactaggart wrote:
    [color=blue]
    > how do I assign a value to a variable inside a function then use it in the
    > main body ?[/color]

    If the variable name is for example 'isbadforyou', start the function with
    the statement:

    global isbadforyou

    [color=blue]
    > i am trying to unpickle a dictionary in a function to use it in the
    > program[/color]

    By far the best approach for this is to have the function work with
    local variables (faster as well as cleaner), preparing the dictionary
    say in local variable 'result', and end the function with:

    return result

    then, the function's caller gets to decide the name (a sounder
    organization!) just by calling the function in some way such as:

    thenameilike = thefunction(its , arguments, ifany)


    Alex

    Comment

    • John Roth

      #3
      Re: global variables ?


      "ruari mactaggart" <ruari@charlief ortune.com> wrote in message
      news:bo04ou$382 $1@news8.svr.po l.co.uk...[color=blue]
      > how do I assign a value to a variable inside a function then use it in the
      > main body ?
      >
      > i am trying to unpickle a dictionary in a function to use it in the[/color]
      program

      Since Alex has already mentioned the 'global' statement,
      I won't dwell on it. I'd just mention that it's a whole lot
      simpler to use a global dictionary or an object instance
      than to use a simple variable that has to be rebound.

      For example:

      foobar = "huh?"

      def snafu():
      global foobar
      foobar = "what?"


      Using a dictionary, it looks like this:

      foodict = {foobar: "huh?"}

      def snafu():
      foodict["foobar"] = "what?"

      That lets you consolidate all of those messy global variables
      in one place as well as giving you a name you can use for
      better internal documentation.

      John Roth[color=blue]
      >
      > ruari
      >
      >[/color]


      Comment

      • ruari mactaggart

        #4
        Re: global variables ?

        thank you ! It works now. This is very satisfying.

        John Roth <newsgroups@jhr othjr.com> wrote in message
        news:vq78f2gc8b gv59@news.super news.com...[color=blue]
        >
        > "ruari mactaggart" <ruari@charlief ortune.com> wrote in message
        > news:bo04ou$382 $1@news8.svr.po l.co.uk...[color=green]
        > > how do I assign a value to a variable inside a function then use it in[/color][/color]
        the[color=blue][color=green]
        > > main body ?
        > >
        > > i am trying to unpickle a dictionary in a function to use it in the[/color]
        > program
        >
        > Since Alex has already mentioned the 'global' statement,
        > I won't dwell on it. I'd just mention that it's a whole lot
        > simpler to use a global dictionary or an object instance
        > than to use a simple variable that has to be rebound.
        >
        > For example:
        >
        > foobar = "huh?"
        >
        > def snafu():
        > global foobar
        > foobar = "what?"
        >
        >
        > Using a dictionary, it looks like this:
        >
        > foodict = {foobar: "huh?"}
        >
        > def snafu():
        > foodict["foobar"] = "what?"
        >
        > That lets you consolidate all of those messy global variables
        > in one place as well as giving you a name you can use for
        > better internal documentation.
        >
        > John Roth[color=green]
        > >
        > > ruari
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Emile van Sebille

          #5
          Re: global variables ?

          Ulrich Petri:[color=blue]
          >
          > Well - no. It's not.
          > Globals are bad, ugly, ...
          >[/color]

          Well, not in and of themselves. They're used in about 200 places in the
          standard distribution, and there's talk (although no plans) of extending the
          syntax further to allow designation of an encompassing (or possible other)
          namespace, as in:

          global var in namespace


          Emile van Sebille
          emile@fenx.com


          Comment

          • Ulrich Petri

            #6
            Re: global variables ?


            "ruari mactaggart" <ruari@charlief ortune.com> schrieb im Newsbeitrag
            news:bo0cuk$sj0 $1@news6.svr.po l.co.uk...[color=blue]
            > John Roth <newsgroups@jhr othjr.com> wrote in message
            > news:vq78f2gc8b gv59@news.super news.com...[color=green]
            > > Using a dictionary, it looks like this:
            > >
            > > foodict = {foobar: "huh?"}
            > >
            > > def snafu():
            > > foodict["foobar"] = "what?"
            > >
            > > That lets you consolidate all of those messy global variables
            > > in one place as well as giving you a name you can use for
            > > better internal documentation.
            > >
            > > John Roth[/color]
            >
            > thank you ! It works now. This is very satisfying.
            >[/color]

            Well - no. It's not.
            Globals are bad, ugly, ...


            Comment

            Working...