zoukankan      html  css  js  c++  java
  • Python中变量的作用域(variable scope)

    http://www.crifan.com/summary_python_variable_effective_scope/

    解释python中变量的作用域

    示例:

    1、代码版

     1 #!/usr/bin/python
     2 # -*- coding: utf-8 -*-
     3 """
     4 -------------------------------------------------------------------------------
     5 Function:
     6 【整理】Python中:self和init__的含义 + 为何要有self和__init__
     7 http://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them
     8  
     9 Author:     Crifan
    10 Verison:    2012-11-27
    11 -------------------------------------------------------------------------------
    12 """
    13  
    14 #注:此处全局的变量名,写成name,只是为了演示而用
    15 #实际上,好的编程风格,应该写成gName之类的名字,以表示该变量是Global的变量
    16 name = "whole global name";
    17  
    18 class Person:
    19     name = "class global name"
    20  
    21     def __init__(self, newPersionName):
    22         #self.name = newPersionName;
    23          
    24         #此处,没有使用self.name
    25         #而使得此处的name,实际上仍是局部变量name
    26         #虽然此处赋值了,但是后面没有被利用到,属于被浪费了的局部变量name
    27         name = newPersionName;
    28  
    29     def sayYourName(self):
    30         #此处,之所以没有像之前一样出现:
    31         #AttributeError: Person instance has no attribute 'name'
    32         #那是因为,虽然当前的实例self中,没有在__init__中初始化对应的name变量,实例self中没有对应的name变量
    33         #但是由于实例所对应的类Person,有对应的name变量,所以也是可以正常执行代码的
    34         #对应的,此处的self.name,实际上是Person.name
    35         print 'My name is %s'%(self.name); # -> class global name
    36         print 'name within class Person is actually the global name: %s'%(name); #-> whole global name
    37         print "only access Person's name via Person.name=%s"%(Person.name); # -> class global name
    38  
    39 def selfAndInitDemo():
    40     persionInstance = Person("crifan");
    41     persionInstance.sayYourName();
    42     print "whole global name is %s"%(name); # -> whole global name
    43      
    44 ###############################################################################
    45 if __name__=="__main__":
    46     selfAndInitDemo();

    2、图解版

    variable effective domain

    3、文字版

      3.1 全局变量 (Global variable)

        不论是其他普通函数中,还是Class类中,都可以通过对应的变量名直接引用。

      3.2 局部变量(Automatic variable)

        对于函数内,包括类Class的函数内,普通的 变量 都是自动临时变量

    4、附:变量的优先级

    普通局部变量和全局变量的作用域,最核心的区别在于:

    内部变量的优先级大于外部变量

    即(函数内的)局部变量优先级大于(外部的)全局变量

    换句话说:

    A、如果函数内部,出现了和全局变量同名的变量,则说明是局部变量;

    B、如果函数内没有同名的局部变量,对应的变量则是全局变量

    这个变量的优先级关系不仅针对python语言,几乎所有语言都适用,比如C/C++/C#等

    所以,上面示例中的__init__中的name,不是全局变量:

    name = “whole global name”;

    中的name而是,局部变量;

    其中,此处是属于那种,Python(和,或其他特殊)语言所特有的,无需声明变量,就可以直接使用的情况。

    即局部变量name,并没有声明,但是就直接拿过来使用,通过:

    name = newPersionName;

    在初始化,设置为对应的名字,此处即”crifan”了。

    5、类(Class)变量

    类的变量,在类的范围内,只能通过

    ClassName.PropertyName (或ClassName.VariableName)去访问当前类ClassNamer的变量PropertyName

    例子中就是通过 Person.name 去访问类Person类中的变量name

    6、示例(Instance)变量

    示例中的变量理论上是用InstanceObject.PropertyName去访问的

    而此处,由于Python中默认的约定俗成写法,把类中函数的第一个参数名定义为Instance变量,且名字叫self,所以就变成了:

    self.PropertyName

    上述类的函数中要想访问Instance变量就可通过 self.name去访问Person类示例中的name变量了

  • 相关阅读:
    107. Binary Tree Level Order Traversal II
    108. Convert Sorted Array to Binary Search Tree
    111. Minimum Depth of Binary Tree
    49. Group Anagrams
    使用MALTAB标定实践记录
    442. Find All Duplicates in an Array
    522. Longest Uncommon Subsequence II
    354. Russian Doll Envelopes
    opencv 小任务3 灰度直方图
    opencv 小任务2 灰度
  • 原文地址:https://www.cnblogs.com/linkenpark/p/5156724.html
Copyright © 2011-2022 走看看