acess class attribute in another file

前端 未结 4 1623
旧巷少年郎
旧巷少年郎 2021-01-24 21:18

I\'m new to python.

I have a question about accessing attribute in class

t1.py

#!/usr/bin/python
import t2

class A:
        flag = False

if __n         


        
4条回答
  •  长发绾君心
    2021-01-24 21:45

    You can find this out yourself:

    t1.py

    #!/usr/bin/python
    import logging
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)
    logger.debug('t1 has started')
    logger.debug('t2 is being imported')
    import t2
    
    logger.debug('A is being "compiled"')
    class A:
        flag = False
    logger.debug('ID A: %r', id(A))
    logger.debug('ID A.flag %r', id(A.flag))
    
    logger.debug('What is __name__? %r', __name__)
    if __name__ == "__main__":
        logger.debug('__name__ was "__main__"')
        logger.debug('Calling t2.f()')
        t2.f()
        logger.debug('t2.f() was called')
        logger.debug('ID A.flag: %r', id(A.flag))
        print(A.flag)
    

    t2.py

    #!/usr/bin/python
    import logging
    logger = logging.getLogger(__name__)
    logger.debug('t2 is being imported')
    
    logger.debug('t2 is now importing t1')
    import t1
    
    def f():
        logger.debug('f is being called')
        t1.A.flag = True
        logger.debug('ID t1: %r', id(t1))
        logger.debug('ID t1.A: %r', id(t1.A))
        logger.debug('ID t1.A.flag: %r', id(t1.A.flag))
        print(t1.A.flag)
    

    My output

    I'm splitting this up with comments

    DEBUG:__main__:t1 has started
    DEBUG:__main__:t2 is being imported
    DEBUG:t2:t2 is being imported
    DEBUG:t2:t2 is now importing t1
    

    As you can see, the first time around (as others have mentioned) t1 actually has the name of __main__. It tries importing t2, but immediately t2 tries importing t1.

    DEBUG:t1:t1 has started
    DEBUG:t1:t2 is being imported
    

    You can see that none of the t2 logging statements run. That's because Python caches imported modules, so it first looks in the cache for t2 and says, "Ahah! I've already imported this guy, I just need to return it. Here you go, then!"

    DEBUG:t1:A is being "compiled"
    DEBUG:t1:ID A: 140377934341704
    DEBUG:t1:ID A.flag 4312040768
    DEBUG:t1:What is __name__? 't1'
    

    So, you'll notice that now it has made its way through importing t1. And t2

    DEBUG:t2:t2 is done being imported
    

    Execution continues back in the __main__ t1

    DEBUG:__main__:A is being "compiled"
    DEBUG:__main__:ID A: 140377934344360
    DEBUG:__main__:ID A.flag 4312040768
    

    Notice that the id for this A and A.flag are different!

    DEBUG:__main__:What is __name__? '__main__'
    DEBUG:__main__:__name__ was "__main__"
    DEBUG:__main__:Calling t2.f()
    DEBUG:t2:f is being called
    DEBUG:t2:ID t1: 4317998840
    DEBUG:t2:ID t1.A: 140377934341704
    DEBUG:t2:ID t1.A.flag: 4312040736
    

    Notice again, that these ids match t1.A's, and not __main__.As.

    True
    DEBUG:__main__:t2.f() was called
    DEBUG:__main__:ID A.flag: 4312040768
    False
    

提交回复
热议问题