Issue defining global variables within functions

Đang mở
#629 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, python
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

IS: limitation IS: workaround STATE: under consideration

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?

Ngôn ngữ chính
Python
Star
2.9k
Fork
218
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TranscryptOrg/Transcrypt

Tất cả issue của TranscryptOrg/Transcrypt

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.