Python - What priority does global have?










3















I'm a bit confused about globals when it comes to packages using other packages. From a quick google search; there's not much that explains.



Simply put: at what level is a variable "globalized" when using global? Is it at the module level, package level, or interpreter level? Ie, in a setup such as this:



<Package>
|- __init__.py
|- Module.py
|- Module2.py


and there is a global statment used within Module.py, is the variable globalized for just that module, or the entire package (including Module2.py and or __init__.py), or at the interperter level (for anything being run in the interpreter).



Also, if in __init__.py I from .Module import *, would the imported functions containing a global statement "properly globalize" any said variable for that file?










share|improve this question

















  • 1





    See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

    – Lukas Graf
    Dec 9 '15 at 0:40
















3















I'm a bit confused about globals when it comes to packages using other packages. From a quick google search; there's not much that explains.



Simply put: at what level is a variable "globalized" when using global? Is it at the module level, package level, or interpreter level? Ie, in a setup such as this:



<Package>
|- __init__.py
|- Module.py
|- Module2.py


and there is a global statment used within Module.py, is the variable globalized for just that module, or the entire package (including Module2.py and or __init__.py), or at the interperter level (for anything being run in the interpreter).



Also, if in __init__.py I from .Module import *, would the imported functions containing a global statement "properly globalize" any said variable for that file?










share|improve this question

















  • 1





    See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

    – Lukas Graf
    Dec 9 '15 at 0:40














3












3








3


1






I'm a bit confused about globals when it comes to packages using other packages. From a quick google search; there's not much that explains.



Simply put: at what level is a variable "globalized" when using global? Is it at the module level, package level, or interpreter level? Ie, in a setup such as this:



<Package>
|- __init__.py
|- Module.py
|- Module2.py


and there is a global statment used within Module.py, is the variable globalized for just that module, or the entire package (including Module2.py and or __init__.py), or at the interperter level (for anything being run in the interpreter).



Also, if in __init__.py I from .Module import *, would the imported functions containing a global statement "properly globalize" any said variable for that file?










share|improve this question














I'm a bit confused about globals when it comes to packages using other packages. From a quick google search; there's not much that explains.



Simply put: at what level is a variable "globalized" when using global? Is it at the module level, package level, or interpreter level? Ie, in a setup such as this:



<Package>
|- __init__.py
|- Module.py
|- Module2.py


and there is a global statment used within Module.py, is the variable globalized for just that module, or the entire package (including Module2.py and or __init__.py), or at the interperter level (for anything being run in the interpreter).



Also, if in __init__.py I from .Module import *, would the imported functions containing a global statement "properly globalize" any said variable for that file?







python globals






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 9 '15 at 0:31









13steinj13steinj

127311




127311







  • 1





    See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

    – Lukas Graf
    Dec 9 '15 at 0:40













  • 1





    See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

    – Lukas Graf
    Dec 9 '15 at 0:40








1




1





See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

– Lukas Graf
Dec 9 '15 at 0:40






See 6.13 The global statement. In short, the global statement only affects the scope it's used in. Also see the relevant part in 6.2 Assignment statements for how it affects assignment.

– Lukas Graf
Dec 9 '15 at 0:40













1 Answer
1






active

oldest

votes


















6














How global are global variables?



So-called "global" variables in Python are really module-level. There are actually-global globals, which live in the __builtin__ module in Python 2 or builtins in Python 3, but you shouldn't touch those. (Also, note the presence or lack of an s. __builtins__ is its own weird thing.)



What does the global statement do?



The global statement means that, for just the function in which it appears, the specified variable name or names refer to the "global" (module-level) variable(s), rather than local variables.



What about import *?



Oh god, don't do that. Globals are bad enough, but importing them is worse, and doing it with import * is just about the worst way you could do it. What the import system does with global variables is horribly surprising to new programmers and almost never what you want.



When you do import *, that doesn't mean that your module starts looking in the imported module's global variables for variable lookup. It means that Python looks in the imported module, finds its "public" global variables*, and assigns their current values to identically-named new global variables in the current module.



That means that any assignments to your new global variables won't affect the originals, and any assignments to the originals won't affect your new variables. Any functions you imported with import * are still looking at the original variables, so they won't see changes you make to your copies, and you won't see changes they make to theirs. The results are a confusing mess.



Seriously, if you absolutely must use another module's global variables, import it with the import othermodule syntax and access the globals with othermodule.whatever_global.




*If the module defines an __all__ list, the "public" globals are the variables whose names appear in that list. Otherwise, they're the variables whose names don't start with an underscore. defined functions are stored in ordinary variables, so those get included under the same criteria as other variables.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34168734%2fpython-what-priority-does-global-have%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    How global are global variables?



    So-called "global" variables in Python are really module-level. There are actually-global globals, which live in the __builtin__ module in Python 2 or builtins in Python 3, but you shouldn't touch those. (Also, note the presence or lack of an s. __builtins__ is its own weird thing.)



    What does the global statement do?



    The global statement means that, for just the function in which it appears, the specified variable name or names refer to the "global" (module-level) variable(s), rather than local variables.



    What about import *?



    Oh god, don't do that. Globals are bad enough, but importing them is worse, and doing it with import * is just about the worst way you could do it. What the import system does with global variables is horribly surprising to new programmers and almost never what you want.



    When you do import *, that doesn't mean that your module starts looking in the imported module's global variables for variable lookup. It means that Python looks in the imported module, finds its "public" global variables*, and assigns their current values to identically-named new global variables in the current module.



    That means that any assignments to your new global variables won't affect the originals, and any assignments to the originals won't affect your new variables. Any functions you imported with import * are still looking at the original variables, so they won't see changes you make to your copies, and you won't see changes they make to theirs. The results are a confusing mess.



    Seriously, if you absolutely must use another module's global variables, import it with the import othermodule syntax and access the globals with othermodule.whatever_global.




    *If the module defines an __all__ list, the "public" globals are the variables whose names appear in that list. Otherwise, they're the variables whose names don't start with an underscore. defined functions are stored in ordinary variables, so those get included under the same criteria as other variables.






    share|improve this answer





























      6














      How global are global variables?



      So-called "global" variables in Python are really module-level. There are actually-global globals, which live in the __builtin__ module in Python 2 or builtins in Python 3, but you shouldn't touch those. (Also, note the presence or lack of an s. __builtins__ is its own weird thing.)



      What does the global statement do?



      The global statement means that, for just the function in which it appears, the specified variable name or names refer to the "global" (module-level) variable(s), rather than local variables.



      What about import *?



      Oh god, don't do that. Globals are bad enough, but importing them is worse, and doing it with import * is just about the worst way you could do it. What the import system does with global variables is horribly surprising to new programmers and almost never what you want.



      When you do import *, that doesn't mean that your module starts looking in the imported module's global variables for variable lookup. It means that Python looks in the imported module, finds its "public" global variables*, and assigns their current values to identically-named new global variables in the current module.



      That means that any assignments to your new global variables won't affect the originals, and any assignments to the originals won't affect your new variables. Any functions you imported with import * are still looking at the original variables, so they won't see changes you make to your copies, and you won't see changes they make to theirs. The results are a confusing mess.



      Seriously, if you absolutely must use another module's global variables, import it with the import othermodule syntax and access the globals with othermodule.whatever_global.




      *If the module defines an __all__ list, the "public" globals are the variables whose names appear in that list. Otherwise, they're the variables whose names don't start with an underscore. defined functions are stored in ordinary variables, so those get included under the same criteria as other variables.






      share|improve this answer



























        6












        6








        6







        How global are global variables?



        So-called "global" variables in Python are really module-level. There are actually-global globals, which live in the __builtin__ module in Python 2 or builtins in Python 3, but you shouldn't touch those. (Also, note the presence or lack of an s. __builtins__ is its own weird thing.)



        What does the global statement do?



        The global statement means that, for just the function in which it appears, the specified variable name or names refer to the "global" (module-level) variable(s), rather than local variables.



        What about import *?



        Oh god, don't do that. Globals are bad enough, but importing them is worse, and doing it with import * is just about the worst way you could do it. What the import system does with global variables is horribly surprising to new programmers and almost never what you want.



        When you do import *, that doesn't mean that your module starts looking in the imported module's global variables for variable lookup. It means that Python looks in the imported module, finds its "public" global variables*, and assigns their current values to identically-named new global variables in the current module.



        That means that any assignments to your new global variables won't affect the originals, and any assignments to the originals won't affect your new variables. Any functions you imported with import * are still looking at the original variables, so they won't see changes you make to your copies, and you won't see changes they make to theirs. The results are a confusing mess.



        Seriously, if you absolutely must use another module's global variables, import it with the import othermodule syntax and access the globals with othermodule.whatever_global.




        *If the module defines an __all__ list, the "public" globals are the variables whose names appear in that list. Otherwise, they're the variables whose names don't start with an underscore. defined functions are stored in ordinary variables, so those get included under the same criteria as other variables.






        share|improve this answer















        How global are global variables?



        So-called "global" variables in Python are really module-level. There are actually-global globals, which live in the __builtin__ module in Python 2 or builtins in Python 3, but you shouldn't touch those. (Also, note the presence or lack of an s. __builtins__ is its own weird thing.)



        What does the global statement do?



        The global statement means that, for just the function in which it appears, the specified variable name or names refer to the "global" (module-level) variable(s), rather than local variables.



        What about import *?



        Oh god, don't do that. Globals are bad enough, but importing them is worse, and doing it with import * is just about the worst way you could do it. What the import system does with global variables is horribly surprising to new programmers and almost never what you want.



        When you do import *, that doesn't mean that your module starts looking in the imported module's global variables for variable lookup. It means that Python looks in the imported module, finds its "public" global variables*, and assigns their current values to identically-named new global variables in the current module.



        That means that any assignments to your new global variables won't affect the originals, and any assignments to the originals won't affect your new variables. Any functions you imported with import * are still looking at the original variables, so they won't see changes you make to your copies, and you won't see changes they make to theirs. The results are a confusing mess.



        Seriously, if you absolutely must use another module's global variables, import it with the import othermodule syntax and access the globals with othermodule.whatever_global.




        *If the module defines an __all__ list, the "public" globals are the variables whose names appear in that list. Otherwise, they're the variables whose names don't start with an underscore. defined functions are stored in ordinary variables, so those get included under the same criteria as other variables.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 9 '15 at 1:08

























        answered Dec 9 '15 at 0:40









        user2357112user2357112

        155k12167260




        155k12167260





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34168734%2fpython-what-priority-does-global-have%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo