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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme