码迷,mamicode.com
首页 > 编程语言 > 详细

python demo整理

时间:2016-11-11 14:13:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:global   div   sel   pytho   with   init   within   锚点   pre   

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整理

标签:global   div   sel   pytho   with   init   within   锚点   pre   

原文地址:http://www.cnblogs.com/balfish/p/6053714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!