AnswerBun.com

How to check local and global scope in Python and override variables in a class?

Stack Overflow Asked by suresh on December 18, 2020

I am learning Python scope and unable to understand with a program, here is my code

a = "bat"
class Test():
    a = "ball"    
    b = [a for i in range(2)]
    e = lambda: a
    @staticmethod
    def f():
        return a
        
    g = lambda a=a: a
    d = a

print(Test.b)  # prints ["bat","bat"]
print(Test.e()) # prints bat
print(Test.f()) # prints bat
print(Test.g()) # prints ball
print(Test.d)  # prints ball

Here I have declared variable a="bat" outside the class and then I declared variable a="ball" inside class which is local to it. When I access the local a="ball" inside the class how is it returning the variable a="bat" declared outside the class in first 3 prints & not in the last 2 prints?

One Answer

When a class is created, there is a new namespace for the class. a (the second one), b, e, f,g and d are all created in the class namespace. Any time you try to access a from within that same namespace, it gets the inner "ball" value, but for any new scope it will first try to find a in its local namespace, then skip the class namespace and use the global namespace.

For d = a, it is inside the class namespace so is "ball". For g = lambda a=a: a, when resolving the a inside the lambda the function, it finds the a in the local namespace which was set to a default value when in the class namespace, so it resolves to "ball".

For all others, they try to access a from a local namespace, but do not find it, so check the global namespace and find it there.

Answered by Oli on December 18, 2020

Add your own answers!

Related Questions

Print [Python] seen as a keyword in Pycharm

1  Asked on February 20, 2021 by jleno46

     

Express req.subdomains empty

1  Asked on February 20, 2021

   

How to exclude data in mySQL string?

2  Asked on February 20, 2021 by markus

     

ERROR at line 3: ORA-00911: invalid character” when joining tables

2  Asked on February 19, 2021 by sinafarheidar12

   

How to transport data from csv to Excel

2  Asked on February 19, 2021 by tangercity

         

How to call Ajax datatables with parameter ASP.NET MVC?

3  Asked on February 19, 2021 by truong-hoang

       

Hide several tds inside the table

2  Asked on February 19, 2021 by tree

     

Kubernetes Conditions.Ready and ContainersReady are false

0  Asked on February 19, 2021 by user3877654

 

Highlight multiple cells using mxCellHighlight

2  Asked on February 18, 2021 by boris

   

How to get all dates up to a certain date?

1  Asked on February 18, 2021 by beck

   

How do I write a script that alerts a input value given by a visitor?

3  Asked on February 18, 2021 by mehmet-akif-vardar

 

Why is “==” not acceptable here?

1  Asked on February 18, 2021 by askman

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP