zoukankan      html  css  js  c++  java
  • 为什么说Python是一门动态语言--Python的魅力

    动态语言的定义:动态编程语言高级程序设计语言的一个类别。在计算机科学领域已被广泛应用。它是一类在执行时能够改变其结构的语言:比如新的函数、对象、甚至代码能够被引进。已有的函数能够被删除或是其它结构上的变化。动态语言眼下很具有活力。众所周知的ECMAScriptJavaScript)便是一个动态语言,除此之外如PHPRubyPython等也都属于动态语言,而CC++等语言则不属于动态语言。

    ----来自维基百科

    你是不是有过给class里面变量赋值却发现程序没达到自己预期结果的遭遇?是不是本来赋值给class.abc却赋给了class.abd?这事实上是动态语言惹的“祸”!【博主曾经玩的是java】我们先来试着玩一玩

    >>> class Person():
    	def __init__(self, name = None, age = None):
    	    self.name = name
    	    self.age = age
    
    	    
    >>> P = Person("The_Third_Wave", "24")
    >>> 

    在这里。我们定义了1个类Person。在这个类里。定义了两个初始属性name和age,可是人还有性别啊。假设这个类不是你写的是不是你会尝试訪问性别这个属性呢?

    >>> P.sexuality = "male"
    >>> P.sexuality
    'male'
    >>> 
    这时候就发现问题了,我们定义的类里面没有sexuality这个属性啊!

    怎么回事呢?这就是动态语言的魅力和坑!

    这里实际上就是动态给实例绑定属性!所以博主“当年”从java转python被“坑”(无知啊)过!我们再看下一个样例

    >>> P1 = Person("Wave", "25")
    >>> P1.sexuality
    
    Traceback (most recent call last):
      File "<pyshell#21>", line 1, in <module>
        P1.sexuality
    AttributeError: Person instance has no attribute 'sexuality'
    >>> 
    我们尝试打印P1.sexuality,发现报错,P1没有sexuality这个属性。----给P这个实例绑定属性对P1这个实例不起作用。

    那我们要给全部的Person的实例加上sexuality属性怎么办呢?答案就是直接给Person绑定属性!

    >>>> Person.sexuality = None
    >>> P1 = Person("Wave", "25")
    >>> print P1.sexuality
    None
    >>> 
    我们直接给Person绑定sexuality这个属性,重行实例化P1后。P1就有sexuality这个属性了!

    那么function呢?怎么绑定?

    >>> class Person():
    	def __init__(self, name = None, age = None):
    	    self.name = name
    	    self.age = age
    	def eat(self):
    	    print "eat food"
    
    	    
    >>> def run(self, speed):
    	print "Keeping moving, the speed is %s km/h" %speed
    
    	
    >>> P = Person("The_Third_Wave", "24")
    >>> 
    KeyboardInterrupt
    >>> P.run()
    
    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in <module>
        P.run()
    AttributeError: Person instance has no attribute 'run'
    >>> P.eat()
    eat food
    >>> import types
    >>> Person.run = types.MethodType(run, None, Person)
    >>> P.run(180)
    Keeping moving, the speed is 180 km/h
    >>> 

    绑定我们了解了,可是怎么删除呢?

    请看下面样例首先给的是属性的真删:

    >>> P.name
    'The_Third_Wave'
    >>> P.sex
    
    Traceback (most recent call last):
      File "<pyshell#32>", line 1, in <module>
        P.sex
    AttributeError: Person instance has no attribute 'sex'
    >>> setattr(P, "sex", "male") # 増
    >>> P.sex
    'male'
    >>> delattr(P, "name") # 删
    >>> P.name
    
    Traceback (most recent call last):
      File "<pyshell#36>", line 1, in <module>
        P.name
    AttributeError: Person instance has no attribute 'name'
    >>> 

    加入方法呢?

    >>> class Person():
    	def __init__(self, name = None, age = None):
    	    self.name = name
    	    self.age = age
    	def eat(self):
    	    print "eat food"
    
    	    
    >>> P = Person("The_Third_Wave", "24")
    >>> P.eat()
    eat food
    >>> P.run()
    
    Traceback (most recent call last):
      File "<pyshell#41>", line 1, in <module>
        P.run()
    AttributeError: Person instance has no attribute 'run'
    >>> def run(self, speed):
    	print "Keeping moving, the speed is %s" %speed
    
    	
    >>> setattr(P, "run", run)
    >>> P.run(360)
    
    Traceback (most recent call last):
      File "<pyshell#45>", line 1, in <module>
        P.run(360)
    TypeError: run() takes exactly 2 arguments (1 given)
    >>> P.run(1, 360)
    Keeping moving, the speed is 360
    >>> 
    删除

    >>> delattr(P, "run")
    >>> P.run()
    
    Traceback (most recent call last):
      File "<pyshell#48>", line 1, in <module>
        P.run()
    AttributeError: Person instance has no attribute 'run'
    >>> 

    通过以上样例能够得出一个结论:相对于动态语言,静态语言具有严谨性!

    所以。玩动态语言的时候,小心动态的坑!

    那么怎么避免这样的情况呢?请使用__slots__。可是我的是2.7.6版本号,測试是不行的。代码例如以下:

    >>> class Person():
    	__slots__ = ("location", "run")
    	
    	def __init__(self, name = None, age = None):
    	    self.name = name
    	    self.age = age
    	    
    	def eat(self):
    	    print "eat food"
    
    	    
    >>> P = Person()
    >>> P.sex
    
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        P.sex
    AttributeError: Person instance has no attribute 'sex'
    >>> P.sex = "male"
    >>> 

    详细原因是什么呢,本来是准备请等待更新:ing...的

    BUT。我多写了个object就出来了。。。

    这可真是个神坑!soga!

    >>> class Person(object):
    	__slots__ = ("location", "run")
    	
    	def __init__(self, name = None, age = None):
    	    self.name = name
    	    self.age = age
    	    
    	def eat(self):
    	    print "eat food"
    
    	    
    >>> P = Person()
    
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        P = Person()
      File "<pyshell#11>", line 5, in __init__
        self.name = name
    AttributeError: 'Person' object has no attribute 'name' # 顺便还发现了个注意事项:要预先定义的属性也要写到tuple里面!

    >>> class Person(object): __slots__ = ("name", "age", "eat", "location", "run") def __init__(self, name = None, age = None): self.name = name self.age = age def eat(self): print "eat food" >>> P = Person() >>> P.sex = "male" Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> P.sex = "male" AttributeError: 'Person' object has no attribute 'sex' >>> P.location = "china" >>> P.location 'china' >>> def run(self, speed): print "Keeping moving, the speed is %s km/h" %speed >>> setattr(P, "run", run) >>> P.run(u"请注意这儿參数和上面有个样例不一样哦", 720) Keeping moving, the speed is 720 km/h >>>

    顺便还发现了个注意事项:要预先定义的属性也要写到tuple里面!

    临时写到这,不定期更新ing...

    关于slots的demo原文:https://docs.python.org/2/reference/datamodel.html?

    highlight=__slots__#__slots__

    本文由@The_Third_Wave原创。不定期更新。有错误请指正。

    Sina微博关注:@The_Third_Wave 

    假设这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏!假设您一定要转载。请带上后缀和本文地址。

查看全文
  • 相关阅读:
    Atitit topic index Abt 150 toic [原]Atitit hi dev eff topic by use dsl sql coll op 提升开发效率sql ds
    Atitit xml转json总结 目录 1.1. XML和JSON之间没有直接映射;元素类型问题 1 1.2. Xml与json的对应关系 2 1.3. 范例 2 2. Jsonlib的问题,,不
    Atitit stomp.js conn连接activemq 目录 1.1. activemq 启动,已经默认开启了stomp ws的接口。。地址是 1 1.2. Js 客户端代码 1 1.3
    Atitit 业务领域体系分类 目录 1. 按照互联网企业类型以及只是体系类的分类 2 2. 电子商务 2 3. **通信类社交 Im类 em 2 4. **信息搜索类爬虫 2 4.1. 媒体
    atitit software sys 软件技术领域工业体系.docx 目录 1. 技术领域一级大类10大类 2 2. 理论与软件设计方法学 2 2.1. 计算机原理 计算机科学导论 2 2.2.
    Atitit api design Usability simple 易用性之简单化设计 目录 1. 理论原则 2 1.1. 概念简单 2 1.2. 切换到了“write less, do more
    Atitit 远程工作的几种办公模式 目录 1. 未来的趋势 远程办公 1 1.1. 遥远的阴影 1 1.2. 一个单中心的团队,是一个团队,每个人都被共处于同一物理位置。 2 1.3. 一个多站
    Atitit 保证产品易用性的方法总结 目录 1. 什么是易用性 易学 易见 三角关系 1 2. 易用性原理 三原则 易见 映射 反馈 2 2.1. 易见 Visibility 可读性 2 2.2.
    Atitit 高级人员要看哪些源码 目录 1. Ati看过的源码 1 1.1. Ui类 1 1.2. Mvc类 1 1.3. 数据库类 1 1.4. 算法类 1 2. 看源码的意义 2 2.1. 一
    Atitit 初级 中级 高级 软件工程师的区别 非功能性需求 目录 1. 初级 业务功能 1 1.1. 中级 独立完成业务功能 已经非常见api功能 更加广阔 1 2. 高级 非功能性需求
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10582607.html
  • Copyright © 2011-2022 走看看