首页 > 代码库 > python demo整理

python demo整理

1 变量作用域

#!/usr/bin/python
# coding=utf-8
name = "whole global name"


class Person:
    name = "class global name"

    def __init__(self, new_name):
        # 执行锚点(1)
        # self.name = new_name

        # 执行锚点(2)
        name = new_name

    def say_your_name(self):
        print My name is %s % self.name  # -> (1) cat  (2) class global name
        print name within class Person is actually the global name: %s % name  # -> (1) whole global name (2) whole global name
        print "only access Person‘s name via Person.name=%s" % Person.name  # -> (1) class global name  (2) class global name


if __name__ == "__main__":
    instance = Person("cat")
    instance.say_your_name()
    print "whole global name is %s" % name  # -> (1) whole global name (2) whole global name

 

python demo整理