pycharm can't reference global variable inside a functionWhat is the Python equivalent of static variables inside a function?How to import a module given its name?Using global variables in a functionHow do I pass a variable by reference?Using global variables between files?how to define a global variable to be used in a function of an imported file?Unresolved reference issue in PyCharmpython mock global function that is used in classUsing variables inside a function in another python fileHow to reset global variable in a subsequent import

New order #4: World

"You are your self first supporter", a more proper way to say it

Why linear maps act like matrix multiplication?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Modification to Chariots for Heavy Cavalry Analogue for 4-armed race

Copenhagen passport control - US citizen

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

How can the DM most effectively choose 1 out of an odd number of players to be targeted by an attack or effect?

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

declaring a variable twice in IIFE

How to make payment on the internet without leaving a money trail?

I see my dog run

What are these boxed doors outside store fronts in New York?

Do Phineas and Ferb ever actually get busted in real time?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Can I interfere when another PC is about to be attacked?

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

How is it possible to have an ability score that is less than 3?

Why don't electromagnetic waves interact with each other?

What defenses are there against being summoned by the Gate spell?

Motorized valve interfering with button?

What is the offset in a seaplane's hull?

How do I create uniquely male characters?



pycharm can't reference global variable inside a function


What is the Python equivalent of static variables inside a function?How to import a module given its name?Using global variables in a functionHow do I pass a variable by reference?Using global variables between files?how to define a global variable to be used in a function of an imported file?Unresolved reference issue in PyCharmpython mock global function that is used in classUsing variables inside a function in another python fileHow to reset global variable in a subsequent import






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I defines a global variable in function.But PyCharm can't reference this global variable.
code like this:



a.py:
g_Handle = None
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a
a.g_handle
# PyCharm will reference 'g_handle' as None,
# but I want reference 'g_handle' as mods.handle_class


I try to add type for g_handle, but I don't want import mods directly in a.py



a.py:
g_handle =None # type: mods.handle_class


but this is not work.can't find mods
so I want to know how to let PyCharm can reference g_handle as mods.handle_class.
Thank you.










share|improve this question
























  • it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

    – lmiguelvargasf
    Mar 9 at 3:53











  • yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

    – sair
    Mar 9 at 4:01











  • a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

    – lmiguelvargasf
    Mar 9 at 4:02











  • em.I'm sorry for didn't explain this question clearly.I have edited the question already.

    – sair
    Mar 9 at 5:02











  • it looks like you will have g_handle set as None until you call Init

    – lmiguelvargasf
    Mar 9 at 5:07

















0















I defines a global variable in function.But PyCharm can't reference this global variable.
code like this:



a.py:
g_Handle = None
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a
a.g_handle
# PyCharm will reference 'g_handle' as None,
# but I want reference 'g_handle' as mods.handle_class


I try to add type for g_handle, but I don't want import mods directly in a.py



a.py:
g_handle =None # type: mods.handle_class


but this is not work.can't find mods
so I want to know how to let PyCharm can reference g_handle as mods.handle_class.
Thank you.










share|improve this question
























  • it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

    – lmiguelvargasf
    Mar 9 at 3:53











  • yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

    – sair
    Mar 9 at 4:01











  • a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

    – lmiguelvargasf
    Mar 9 at 4:02











  • em.I'm sorry for didn't explain this question clearly.I have edited the question already.

    – sair
    Mar 9 at 5:02











  • it looks like you will have g_handle set as None until you call Init

    – lmiguelvargasf
    Mar 9 at 5:07













0












0








0








I defines a global variable in function.But PyCharm can't reference this global variable.
code like this:



a.py:
g_Handle = None
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a
a.g_handle
# PyCharm will reference 'g_handle' as None,
# but I want reference 'g_handle' as mods.handle_class


I try to add type for g_handle, but I don't want import mods directly in a.py



a.py:
g_handle =None # type: mods.handle_class


but this is not work.can't find mods
so I want to know how to let PyCharm can reference g_handle as mods.handle_class.
Thank you.










share|improve this question
















I defines a global variable in function.But PyCharm can't reference this global variable.
code like this:



a.py:
g_Handle = None
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a
a.g_handle
# PyCharm will reference 'g_handle' as None,
# but I want reference 'g_handle' as mods.handle_class


I try to add type for g_handle, but I don't want import mods directly in a.py



a.py:
g_handle =None # type: mods.handle_class


but this is not work.can't find mods
so I want to know how to let PyCharm can reference g_handle as mods.handle_class.
Thank you.







python pycharm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 4:59







sair

















asked Mar 9 at 3:46









sairsair

12




12












  • it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

    – lmiguelvargasf
    Mar 9 at 3:53











  • yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

    – sair
    Mar 9 at 4:01











  • a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

    – lmiguelvargasf
    Mar 9 at 4:02











  • em.I'm sorry for didn't explain this question clearly.I have edited the question already.

    – sair
    Mar 9 at 5:02











  • it looks like you will have g_handle set as None until you call Init

    – lmiguelvargasf
    Mar 9 at 5:07

















  • it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

    – lmiguelvargasf
    Mar 9 at 3:53











  • yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

    – sair
    Mar 9 at 4:01











  • a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

    – lmiguelvargasf
    Mar 9 at 4:02











  • em.I'm sorry for didn't explain this question clearly.I have edited the question already.

    – sair
    Mar 9 at 5:02











  • it looks like you will have g_handle set as None until you call Init

    – lmiguelvargasf
    Mar 9 at 5:07
















it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

– lmiguelvargasf
Mar 9 at 3:53





it is like you are not defining g_handle in your file a.py. You should define it at the zero-indentation level.

– lmiguelvargasf
Mar 9 at 3:53













yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

– sair
Mar 9 at 4:01





yes . but I don't want assignt it at the zero-indentation level.Only want assign it in function. And I want PyCharm want reference g_handle as mods.handle_class.

– sair
Mar 9 at 4:01













a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

– lmiguelvargasf
Mar 9 at 4:02





a global variable by definition should be declared at the zero-indentation level. Otherwise, we are talking about local variables inside a function which are not accessible in other functions due scoping rules.

– lmiguelvargasf
Mar 9 at 4:02













em.I'm sorry for didn't explain this question clearly.I have edited the question already.

– sair
Mar 9 at 5:02





em.I'm sorry for didn't explain this question clearly.I have edited the question already.

– sair
Mar 9 at 5:02













it looks like you will have g_handle set as None until you call Init

– lmiguelvargasf
Mar 9 at 5:07





it looks like you will have g_handle set as None until you call Init

– lmiguelvargasf
Mar 9 at 5:07












1 Answer
1






active

oldest

votes


















0














I'm not sure if it's an element of the way you're asking your question, but it looks like you've got multiple problems here. The first is an import reference problem (or possibly using an import where a class would be more effective).



See, if you run the code only as you have described and provided, you would never get the correct answer that you seek because the Init function in a.py would never get called.



You need to have the intended global variable defined in some way in the global scope prior to being used like in your code.



The global statement merely tells the interpreter to link the values of the provided variable across all states. It does not define the variable in the outermost scope on its own.



Therefore, something like this (EDIT: FIXED):



a.py:
g_handle = False
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a

a.Init()
a.g_handle


...should work to return what you're looking for.



If you could use a class instead of importing from another module, you could save yourself from trouble too:



import mods

class a():
g_handle = False
global g_handle

def __init__(self, handle_class):
g_handle = handle_class()

if __name__ == "__main__":
a(mods.handle_class).g_handle





share|improve this answer

























  • but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

    – sair
    Mar 9 at 4:55












  • sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

    – etherwar
    Mar 10 at 12:46











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%2f55073788%2fpycharm-cant-reference-global-variable-inside-a-function%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









0














I'm not sure if it's an element of the way you're asking your question, but it looks like you've got multiple problems here. The first is an import reference problem (or possibly using an import where a class would be more effective).



See, if you run the code only as you have described and provided, you would never get the correct answer that you seek because the Init function in a.py would never get called.



You need to have the intended global variable defined in some way in the global scope prior to being used like in your code.



The global statement merely tells the interpreter to link the values of the provided variable across all states. It does not define the variable in the outermost scope on its own.



Therefore, something like this (EDIT: FIXED):



a.py:
g_handle = False
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a

a.Init()
a.g_handle


...should work to return what you're looking for.



If you could use a class instead of importing from another module, you could save yourself from trouble too:



import mods

class a():
g_handle = False
global g_handle

def __init__(self, handle_class):
g_handle = handle_class()

if __name__ == "__main__":
a(mods.handle_class).g_handle





share|improve this answer

























  • but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

    – sair
    Mar 9 at 4:55












  • sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

    – etherwar
    Mar 10 at 12:46















0














I'm not sure if it's an element of the way you're asking your question, but it looks like you've got multiple problems here. The first is an import reference problem (or possibly using an import where a class would be more effective).



See, if you run the code only as you have described and provided, you would never get the correct answer that you seek because the Init function in a.py would never get called.



You need to have the intended global variable defined in some way in the global scope prior to being used like in your code.



The global statement merely tells the interpreter to link the values of the provided variable across all states. It does not define the variable in the outermost scope on its own.



Therefore, something like this (EDIT: FIXED):



a.py:
g_handle = False
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a

a.Init()
a.g_handle


...should work to return what you're looking for.



If you could use a class instead of importing from another module, you could save yourself from trouble too:



import mods

class a():
g_handle = False
global g_handle

def __init__(self, handle_class):
g_handle = handle_class()

if __name__ == "__main__":
a(mods.handle_class).g_handle





share|improve this answer

























  • but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

    – sair
    Mar 9 at 4:55












  • sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

    – etherwar
    Mar 10 at 12:46













0












0








0







I'm not sure if it's an element of the way you're asking your question, but it looks like you've got multiple problems here. The first is an import reference problem (or possibly using an import where a class would be more effective).



See, if you run the code only as you have described and provided, you would never get the correct answer that you seek because the Init function in a.py would never get called.



You need to have the intended global variable defined in some way in the global scope prior to being used like in your code.



The global statement merely tells the interpreter to link the values of the provided variable across all states. It does not define the variable in the outermost scope on its own.



Therefore, something like this (EDIT: FIXED):



a.py:
g_handle = False
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a

a.Init()
a.g_handle


...should work to return what you're looking for.



If you could use a class instead of importing from another module, you could save yourself from trouble too:



import mods

class a():
g_handle = False
global g_handle

def __init__(self, handle_class):
g_handle = handle_class()

if __name__ == "__main__":
a(mods.handle_class).g_handle





share|improve this answer















I'm not sure if it's an element of the way you're asking your question, but it looks like you've got multiple problems here. The first is an import reference problem (or possibly using an import where a class would be more effective).



See, if you run the code only as you have described and provided, you would never get the correct answer that you seek because the Init function in a.py would never get called.



You need to have the intended global variable defined in some way in the global scope prior to being used like in your code.



The global statement merely tells the interpreter to link the values of the provided variable across all states. It does not define the variable in the outermost scope on its own.



Therefore, something like this (EDIT: FIXED):



a.py:
g_handle = False
def Init():
import mods
global g_handle
g_handle = mods.handle_class()

b.py:
import a

a.Init()
a.g_handle


...should work to return what you're looking for.



If you could use a class instead of importing from another module, you could save yourself from trouble too:



import mods

class a():
g_handle = False
global g_handle

def __init__(self, handle_class):
g_handle = handle_class()

if __name__ == "__main__":
a(mods.handle_class).g_handle






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 10 at 12:54

























answered Mar 9 at 4:38









etherwaretherwar

8319




8319












  • but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

    – sair
    Mar 9 at 4:55












  • sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

    – etherwar
    Mar 10 at 12:46

















  • but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

    – sair
    Mar 9 at 4:55












  • sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

    – etherwar
    Mar 10 at 12:46
















but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

– sair
Mar 9 at 4:55






but PyCharm can't reference a.g_handle as mods.handle_class alse, and no have smart code completion.I'm sorry for didn't explain this question clearly.

– sair
Mar 9 at 4:55














sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

– etherwar
Mar 10 at 12:46





sorry I made a mistake in my code. it should work now. I changed g = False to g_handle = False

– etherwar
Mar 10 at 12:46



















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%2f55073788%2fpycharm-cant-reference-global-variable-inside-a-function%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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page