Assigning a value to a global variable inside a function [duplicate]












-2
















This question already has an answer here:




  • Don't understand why UnboundLocalError occurs [duplicate]

    8 answers




a = 2
def alter_a():
a = a * 2
return a


Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?



Sssign a new value to a... which is the old value * 2. Why isn't this possible?










share|improve this question













marked as duplicate by juanpa.arrivillaga python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 9:17


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -2
















    This question already has an answer here:




    • Don't understand why UnboundLocalError occurs [duplicate]

      8 answers




    a = 2
    def alter_a():
    a = a * 2
    return a


    Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?



    Sssign a new value to a... which is the old value * 2. Why isn't this possible?










    share|improve this question













    marked as duplicate by juanpa.arrivillaga python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 23 '18 at 9:17


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -2












      -2








      -2









      This question already has an answer here:




      • Don't understand why UnboundLocalError occurs [duplicate]

        8 answers




      a = 2
      def alter_a():
      a = a * 2
      return a


      Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?



      Sssign a new value to a... which is the old value * 2. Why isn't this possible?










      share|improve this question















      This question already has an answer here:




      • Don't understand why UnboundLocalError occurs [duplicate]

        8 answers




      a = 2
      def alter_a():
      a = a * 2
      return a


      Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?



      Sssign a new value to a... which is the old value * 2. Why isn't this possible?





      This question already has an answer here:




      • Don't understand why UnboundLocalError occurs [duplicate]

        8 answers








      python python-3.x scope






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 9:15









      BrowserMBrowserM

      214




      214




      marked as duplicate by juanpa.arrivillaga python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 9:17


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by juanpa.arrivillaga python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 9:17


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          The a inside the function lives inside the function scope, if you want to reference the outer one use global:



          a = 2
          def alter_a():
          global a
          a = a * 2





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The a inside the function lives inside the function scope, if you want to reference the outer one use global:



            a = 2
            def alter_a():
            global a
            a = a * 2





            share|improve this answer




























              2














              The a inside the function lives inside the function scope, if you want to reference the outer one use global:



              a = 2
              def alter_a():
              global a
              a = a * 2





              share|improve this answer


























                2












                2








                2







                The a inside the function lives inside the function scope, if you want to reference the outer one use global:



                a = 2
                def alter_a():
                global a
                a = a * 2





                share|improve this answer













                The a inside the function lives inside the function scope, if you want to reference the outer one use global:



                a = 2
                def alter_a():
                global a
                a = a * 2






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 9:17









                NetwaveNetwave

                12.5k22144




                12.5k22144

















                    Popular posts from this blog

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno