Hướng dẫn how to change a variable outside of a function python - cách thay đổi một biến bên ngoài một hàm python

Đây là một giải pháp đơn giản nhưng bẩn thỉu: Sử dụng một biến có thể thay đổi.

Thay vì

currentMovie = 0

def UpdateText[]:
    currentMovie = random.randint[0, 100]
    print[currentMovie]

Bạn có thể sử dụng danh sách đơn bào cho CurrentMovie và chuyển nó dưới dạng đối số [mặc định] cho UpdateText []:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number

Lưu ý rằng cài đặt

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
3 chính nó [không phải nội dung của nó] với một giá trị mới, ngay cả với danh sách mới, sẽ khiến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
4 ngừng cập nhật ____23 trừ khi khối
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
6 được chạy lại.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed

Đây là một thủ thuật tiện dụng hơn nếu bạn đang xây dựng một cái gì đó nhanh chóng và bẩn thỉu và không muốn xây dựng một lớp học; Tôi thấy rằng Python là tuyệt vời cho những điều như vậy, vì vậy tôi nghĩ rằng điều này vẫn đáng để chia sẻ bất chấp những câu trả lời khác.

Tuy nhiên, đối với các mục đích nghiêm trọng hơn, việc tạo ra một lớp học như trong câu trả lời của Bruno gần như chắc chắn sẽ tốt hơn.

  1. Sử dụng
    currentMovie = [0]
    
    def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
        cM[0] = random.randint[0, 100] # This will change the *contents* of the list
        print[cM[0]] # I used a different name for the parameter to distinguish the two
    
    currentMovie = 3 # UpdateText[] will no longer affect this variable at all
    
    # This will thus not throw an error, since it's modifying the 'old' currentMovie list:
    UpdateText[] # The contents of this list can also no longer be accessed
    
    7 và thay đổi chúng từ một chức năng trong Python
  2. Tạo biến
    currentMovie = [0]
    
    def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
        cM[0] = random.randint[0, 100] # This will change the *contents* of the list
        print[cM[0]] # I used a different name for the parameter to distinguish the two
    
    currentMovie = 3 # UpdateText[] will no longer affect this variable at all
    
    # This will thus not throw an error, since it's modifying the 'old' currentMovie list:
    UpdateText[] # The contents of this list can also no longer be accessed
    
    8 trong Python
  3. Thay đổi giá trị của biến
    currentMovie = [0]
    
    def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
        cM[0] = random.randint[0, 100] # This will change the *contents* of the list
        print[cM[0]] # I used a different name for the parameter to distinguish the two
    
    currentMovie = 3 # UpdateText[] will no longer affect this variable at all
    
    # This will thus not throw an error, since it's modifying the 'old' currentMovie list:
    UpdateText[] # The contents of this list can also no longer be accessed
    
    8 từ một hàm trong Python
  4. Từ khóa
    currentMovie = [0]
    
    def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
        cM[0] = random.randint[0, 100] # This will change the *contents* of the list
        print[cM[0]] # I used a different name for the parameter to distinguish the two
    
    currentMovie = 3 # UpdateText[] will no longer affect this variable at all
    
    # This will thus not throw an error, since it's modifying the 'old' currentMovie list:
    UpdateText[] # The contents of this list can also no longer be accessed
    
    8 trong Python
  5. nhiều chức năng và biến toàn cầu
  6. Ở đây, chúng tôi có một biến toàn cầu
    x = 10
    def avengers[]:
        print["From avengers:", x]
    
    avengers[]
    print["Outside avengers:", x]
    
    9 với giá trị
    From avengers: 10
    Outside avengers: 10
    
    0. Xem cách hàm
    From avengers: 10
    Outside avengers: 10
    
    1 sử dụng từ khóa
    Gibberish
    
    NameError: name 'x' is not defined
    
    1 để sửa đổi giá trị của
    x = 10
    def avengers[]:
        print["From avengers:", x]
    
    avengers[]
    print["Outside avengers:", x]
    
    9.
  7. Sự kết luận

Hướng dẫn này đã dạy những điều cơ bản của các biến

Gibberish

NameError: name 'x' is not defined
1 trong Python. Chúng tôi đã thấy chúng khác với các biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
15 và cách chúng tôi tạo chúng trong Python.

Sử dụng
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
7 và thay đổi chúng từ một chức năng trong Python

Đầu tiên, khai báo biến

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5.

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]

Output:

Gibberish

NameError: name 'x' is not defined

Biến

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 nằm trong hàm
def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
7. Điều này có nghĩa là phạm vi của biến này chỉ giới hạn trong chức năng này.

Đó là lý do tại sao chúng tôi gặp lỗi nếu chúng tôi cố gắng truy cập biến này bên ngoài chức năng này.

Tạo biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
8 trong Python

Di chuyển Biến

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 khai báo bên ngoài hàm.

x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]

Output:

Gibberish
10

Biến

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 được xác định bên ngoài hàm và do đó, chúng ta có thể sử dụng nó ở bất cứ đâu trong chương trình của chúng tôi.

Ngoài ra, nó là một biến

Gibberish

NameError: name 'x' is not defined
1. Tuyên bố một biến trong phạm vi toàn cầu tạo ra một biến
Gibberish

NameError: name 'x' is not defined
1 trong Python.

Chúng tôi cũng có thể truy cập biến

Gibberish

NameError: name 'x' is not defined
1
def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 từ hàm
def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
7.

x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]

Output:

From avengers: 10
Outside avengers: 10

Thay đổi giá trị của biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
8 từ một hàm trong Python

Mã này có biến

Gibberish

NameError: name 'x' is not defined
1
def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 với
Gibberish

NameError: name 'x' is not defined
9. Sau đó, bên trong hàm
x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]
0, chúng tôi thêm
x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]
1 vào biến này
def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5.

Một câu lệnh in bên trong hàm sẽ in giá trị cập nhật của

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5.

x = 10
def change[]:
    x = x + 12
    print[x]

change[]

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
0

Trong Python, một hàm chỉ có thể truy cập và in

x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]
4. Chúng ta cần nói chức năng đề cập đến bất kỳ nhiệm vụ hoặc thay đổi nào thành
x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]
4.

Nếu chúng ta không chỉ định điều này, chức năng nghĩ rằng các bài tập và thay đổi được thực hiện cho chính biến cục bộ. Vì vậy, chúng tôi nhận được lỗi này.

Sử dụng từ khóa

Gibberish

NameError: name 'x' is not defined
1 để thay đổi giá trị
x = 10
def avengers[]:
    print["Gibberish"]

avengers[]
print[x]
4 từ bên trong hàm Python.

Từ khóa
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
8 trong Python

Python cung cấp cho bạn một từ khóa có tên

Gibberish

NameError: name 'x' is not defined
1 để sửa đổi một biến bên ngoài phạm vi của nó. Sử dụng nó khi bạn phải thay đổi giá trị của một biến hoặc thực hiện bất kỳ bài tập nào.

Hãy để chúng tôi thử sửa mã trên bằng từ khóa

Gibberish

NameError: name 'x' is not defined
1.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
1

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
2

Xem cách chúng tôi chỉ định

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 là toàn cầu bằng cách sử dụng từ khóa
Gibberish

NameError: name 'x' is not defined
1 trong dòng thứ ba.

Bây giờ, chúng ta hãy xem giá trị của biến

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 khi in nó bên ngoài hàm.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
3

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
4

Vì chức năng đã cập nhật

def avengers[]:
    x = 10
    print["Gibberish"]

avengers[]
print[x]
5 từ
Gibberish

NameError: name 'x' is not defined
9 đến
Gibberish
10
6, chúng tôi sẽ nhận được giá trị cập nhật dưới dạng đầu ra ngay cả khi truy cập biến bên ngoài phạm vi cục bộ.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
5

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
6

Suy luận tốt nhất bạn có thể rút ra từ đầu ra này là - vấn đề thứ tự. Hàm

Gibberish
10
7 sử dụng giá trị ban đầu của
Gibberish
10
8 chứ không phải giá trị sửa đổi.

Điều này là do hàm

Gibberish
10
9 sửa đổi giá trị của
Gibberish
10
8 được gọi theo hàm
Gibberish
10
7. Nếu chúng ta gọi hàm
Gibberish
10
9 trước, hàm
Gibberish
10
7 cũng sẽ sử dụng giá trị cập nhật là
Gibberish
10
8.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
7

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
8

Điều này đưa chúng ta đến một số quy tắc mà bạn phải tuân theo trong khi sử dụng từ khóa Python từ

Gibberish

NameError: name 'x' is not defined
1.

  • Theo mặc định, một biến bên trong một hàm là cục bộ và một biến bên ngoài hàm là
    Gibberish
    
    NameError: name 'x' is not defined
    
    1. Don Tiết sử dụng từ khóa này cho các biến bên ngoài một hàm.
  • Sử dụng từ khóa
    Gibberish
    
    NameError: name 'x' is not defined
    
    1 bên ngoài một chức năng trong Python không ảnh hưởng đến mã theo bất kỳ cách nào.
  • Việc sử dụng chính của từ khóa
    Gibberish
    
    NameError: name 'x' is not defined
    
    1 là thực hiện các bài tập hoặc thay đổi trong Python. Vì vậy, chúng tôi không cần nó để chỉ truy cập hoặc in biến.

nhiều chức năng và biến toàn cầu

Ở đây, chúng tôi có một biến toàn cầu

x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9 với giá trị
From avengers: 10
Outside avengers: 10
0. Xem cách hàm
From avengers: 10
Outside avengers: 10
1 sử dụng từ khóa
Gibberish

NameError: name 'x' is not defined
1 để sửa đổi giá trị của
x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
9

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
0

Đầu tiên, chúng tôi gọi hàm

From avengers: 10
Outside avengers: 10
1. Hàm này sửa đổi biến toàn cầu
x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9 và thay đổi nó thành
From avengers: 10
Outside avengers: 10
6.

Chúng tôi nhận được đầu ra là

From avengers: 10
Outside avengers: 10
6 từ câu lệnh in đầu tiên. Sau đó, chúng tôi gọi hàm
From avengers: 10
Outside avengers: 10
8.

Chúng tôi một lần nữa gọi hàm

From avengers: 10
Outside avengers: 10
8 bên trong hàm
From avengers: 10
Outside avengers: 10
1. Lần này, hàm
From avengers: 10
Outside avengers: 10
1 cũng sửa đổi giá trị của biến
x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9.

Nó lấy giá trị trước đó là

From avengers: 10
Outside avengers: 10
6 và sau đó cập nhật nó lên
x = 10
def change[]:
    x = x + 12
    print[x]

change[]
4. Vì vậy, giá trị cuối cùng của biến toàn cầu bây giờ trở thành
x = 10
def change[]:
    x = x + 12
    print[x]

change[]
4.

Sau đó, hàm

From avengers: 10
Outside avengers: 10
8 sửa đổi nó, nhưng điều này sẽ không được cập nhật trong biến toàn cầu. Nó sử dụng giá trị cập nhật của
x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9 và in giá trị sau khi thêm
Gibberish

NameError: name 'x' is not defined
9.

Nó không sử dụng từ khóa

Gibberish

NameError: name 'x' is not defined
1. Do đó, đầu ra
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
00. Bây giờ bạn có thể thấy lý do tại sao đầu ra của câu lệnh cuối cùng là
x = 10
def change[]:
    x = x + 12
    print[x]

change[]
4.

Đây không là gì ngoài giá trị cập nhật của biến toàn cầu

x = 10
def avengers[]:
    print["From avengers:", x]

avengers[]
print["Outside avengers:", x]
9.

một hàm có biến có cùng tên với biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
8

Có khả năng chúng tôi có một chức năng có một biến được khai báo bên trong nó với cùng tên với biến

Gibberish

NameError: name 'x' is not defined
1.

Một câu hỏi không thể tránh khỏi phát sinh ở đây là - chức năng sẽ sử dụng biến nào? Địa phương hay toàn cầu? Để xem nào.

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
1

Output:

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the list
    print[cM[0]] # I used a different name for the parameter to distinguish the two

currentMovie = 3 # UpdateText[] will no longer affect this variable at all

# This will thus not throw an error, since it's modifying the 'old' currentMovie list:
UpdateText[] # The contents of this list can also no longer be accessed
2

Có một biến

Gibberish

NameError: name 'x' is not defined
1
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
06 trong mã này, có giá trị là
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
07. Hàm
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
08 cũng có một biến có tên
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
06.

Giá trị của biến này là

Gibberish

NameError: name 'x' is not defined
9. Khi chúng ta truy cập giá trị của biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
06 bên trong hàm, chúng ta sẽ nhận được giá trị của biến cục bộ cho hàm này, đó là
Gibberish

NameError: name 'x' is not defined
9.

Khi chúng tôi truy cập giá trị của

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
06 từ bên ngoài chức năng này, chúng tôi sẽ nhận được đầu ra là
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
07.

Điều này ngụ ý rằng nếu biến

currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
15 có cùng tên với biến
Gibberish

NameError: name 'x' is not defined
1 trong một phạm vi cụ thể, thì nó có mức độ ưu tiên hơn biến toàn cầu.

Sự kết luận

Hướng dẫn này đã dạy những điều cơ bản của các biến

Gibberish

NameError: name 'x' is not defined
1 trong Python. Chúng tôi đã thấy chúng khác với các biến
currentMovie = [0]

def UpdateText[cM=currentMovie]: # The default value will 'bind' currentMovie to this argument
    cM[0] = random.randint[0, 100] # This will change the *contents* of the variable
    print[cM[0]] # I used a different name for the parameter to distinguish the two

UpdateText[] # Calls UpdateText, updating the contents of currentMovie with a random number
15 và cách chúng tôi tạo chúng trong Python.

Làm thế nào để bạn đặt một biến bên ngoài một hàm trong Python?

Sử dụng câu lệnh toàn cầu nếu bạn muốn gán một giá trị cho một tên được xác định bên ngoài hàm, thì bạn phải nói với Python rằng tên này không phải là cục bộ, nhưng nó là toàn cầu. Chúng tôi làm điều này bằng cách sử dụng tuyên bố toàn cầu. Không thể gán một giá trị cho một biến được xác định bên ngoài một hàm mà không có tuyên bố toàn cầu. If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.

Có thể thay đổi giá trị biến trong Python?

Các loại đột biến và bất biến Một số giá trị trong Python có thể được sửa đổi, và một số không thể.Điều này không bao giờ có nghĩa là chúng ta không thể thay đổi giá trị của một biến - nhưng nếu một biến chứa một giá trị của một loại bất biến, chúng ta chỉ có thể gán cho nó một giá trị mới.Chúng ta không thể thay đổi giá trị hiện có theo bất kỳ cách nào.Some values in python can be modified, and some cannot. This does not ever mean that we can't change the value of a variable – but if a variable contains a value of an immutable type, we can only assign it a new value. We cannot alter the existing value in any way.

Làm thế nào để bạn sử dụng một biến bên ngoài một hàm bên trong một hàm?

Bạn phải khai báo biến bên ngoài chức năng để sử dụng nó.Các biến được khai báo bên trong một hàm chỉ có thể được truy cập bên trong hàm đó.Hãy nghĩ về từng chức năng như một hộp và nắp chỉ có thể được mở từ bên trong: chức năng có thể lấy những gì bên ngoài hoặc đặt một cái gì đó ra, nhưng bạn không thể đạt được trong hộp của nó.declare the variable outside the function to use it. variables declared inside a function can only be accessed inside that function. think of each function as a box and the lid can only be opened from inside: the function can grab what's outside or put something out, but you can't reach in its box.

Bạn có thể sử dụng một biến bên ngoài một chức năng không?

Các biến được khai báo bên ngoài bất kỳ chức năng nào trở thành biến toàn cầu.Các biến toàn cầu có thể được truy cập và sửa đổi từ bất kỳ chức năng nào.Trong ví dụ trên, tên người dùng biến trở thành biến toàn cầu vì nó được khai báo bên ngoài bất kỳ chức năng nào.. Global variables can be accessed and modified from any function. In the above example, the variable userName becomes a global variable because it is declared outside of any function.

Bài Viết Liên Quan

Chủ Đề