首页 > 代码库 > 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整理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。