Issue defining global variables within functions
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- javascript, python
- Domain
- compilers
Research direction
No repository files or tests are named. Start by reproducing the two global-variable examples and inspecting the JavaScript generated for each, then trace the compiler behavior for Python global assignments and the globals() form. Done means either supporting the requested behavior with regression coverage or clearly documenting the supported workaround.
Written by the indexing model from the issue text.
Description
Hi! It's me again =]
I'm having an issue in my pyp5js project releated to not being able to create new global variables from inside a function. For example, the following pure Python code works:
def foo():
global x
x = 30
print(x)
foo()
>>> 30
But if an equivalent version to be browser, such as the following example, doesn't:
def foo():
global my_var
my_var = 10
console.log(my_var)
foo()
The error I get from the browser console is: ReferenceError: assignment to undeclared variable my_var at line 3. But, if I declare the global variable my_var outside the foo function, it works and prints "10" in the console:
my_var = None
def foo():
global my_var
my_var = 10
console.log(my_var)
foo()
Studying transcrypt's docs, I got to this page about the use of globals function and then I tried the following example, which didn't work either:
# __pragma__ ('xglobs')
def foo():
globals () ["my_var"] = 10
console.log("inside function", my_var)
foo()
console.log("outside function", my_var)
This one raises a ReferenceError: my_var is not defined at line 3 as well.
So, I'd like to know if is there'a way to respect regular Python's usage of global or, given the restrictions of compiling the code to JS, to use global variables should I have to declare them at the beginning of the module, before assigning values?
- Dominant language
- Python
- Stars
- 2.9k
- Forks
- 218
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TranscryptOrg/Transcrypt
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
TranscryptOrg/Transcrypt#913 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
TranscryptOrg/Transcrypt#911 · 2 comments ·
-
IS: bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
TranscryptOrg/Transcrypt#908 ·
-
SUB: documentation
Difficulty 1/5 Under an hour Newbie friendliness 62/100
TranscryptOrg/Transcrypt#656 · 7 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 76/100
TranscryptOrg/Transcrypt#914 ·
All issues in TranscryptOrg/Transcrypt
Similar issues
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100