为什么说Python是一门动态语言--Python的魅力
动态语言的定义:动态编程语言是高级程序设计语言的一个类别,在计算机科学领域已被广泛应用。
它是一类在执行时能够改变其结构的语言:比如新的函数、对象、甚至代码能够被引进,已有的函数能够被删除或是其它结构上的变化。
动态语言眼下很具有活力。
众所周知的ECMAScript(JavaScript)便是一个动态语言。除此之外如PHP、Ruby、Python等也都属于动态语言,而C、C++等语言则不属于动态语言。----来自维基百科
你是不是有过给class里面变量赋值却发现程序没达到自己预期结果的遭遇?是不是本来赋值给class.abc却赋给了class.abd?这事实上是动态语言惹的“祸”!
【博主曾经玩的是java】我们先来试着玩一玩
>>> class Person():def __init__(self, name = None, age = None):self.name = nameself.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.sexualityTraceback (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 = nameself.age = agedef 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.sexTraceback (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.nameTraceback (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 = nameself.age = agedef 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 = nameself.age = agedef eat(self):print "eat food">>> P = Person()
>>> P.sexTraceback (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 = nameself.age = agedef 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
假设这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏。假设您一定要转载,请带上后缀和本文地址。
为什么说Python是一门动态语言--Python的魅力相关推荐
- p语言是python吗-为什么说Python是一门动态语言--Python的魅力
给P这个实例绑定属性对P1这个实例不起作用! 那我们要给全部的Person的实例加上sexuality属性怎么办呢?答案就是直接给Person绑定属性! >>>> Person ...
- [转载] 【Python进阶】4-2 多态 | 什么是多态 / 静态语言vs动态语言 / python中多态
参考链接: Python中的多态 文章目录 1.什么是多态"开闭"原则 2.静态语言 vs 动态语言小结 3.python中多态 1.什么是多态 要理解什么是多态,我们首先要对数据 ...
- Python 是一门动态的、强类型语言
1. 什么是动态语言? 在了解动态语言之前,我们首先了解下 "类型检查". 类型检查是验证类型约束的过程,编译器或解释器通常在编译阶段或运行阶段做类型检查. 类型检查就是查看 &q ...
- python语言的作者是_Python是一门动态语言
[IT168 评论]动态语言的定义 动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用.它是一类 在运行时可以改变其结构的语言 :例如新的函数.对象.甚至代码可以被引进,已有 ...
- python是高级动态语言_Python动态语言之魅力揭秘
之前的文章跟大家讲解了鸭子类型,其实鸭子类型是编程语言中动态类型语言中的一种设计风格.今天跟大家一起谈谈动态语言的魅力. 根据维基百科,动态编程语言是这样子定义的:动态编程语言是高级编程语言的一个类别 ...
- python是高级动态编程语言-Python语言
Python是一门跨平台.开源.免费的解释型高级动态编程语言. Python支持命令式编程(How to do).函数式编程(What to do),完全支持面向对象程序设计,拥有大量扩展库. 胶水语 ...
- python是一门什么课程-Python究竟是一门怎样的语言,Python为什么这么火?
Python究竟是一门怎样的语言? Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些 ...
- python为什么是动态语言_python为什么是动态语言
首先要理解什么是动态语言:通俗地说:能够在运行时修改自身程序结构的语言,就属于动态语言.那怎样才算是"运行时修改自身程序结构"呢?比如下面这几个例子都算:在运行时给某个类增加成员函 ...
- lua语言和python_[动态语言]python和lua中的三元操作符and-or
在这两种语言中,表达式a and b的返回值不是true或false,而是a/b当中非真的值,而表示a or b返回的是a/b当中为真的那个. 因此,要想模拟C/C++中的三元操作符c ? a : b ...
最新文章
- 情感分析:基于卷积神经网络
- 放大器和比较器的区别
- MySQL学习笔记 约束以及修改数据表
- 如何理解Linux shell中的“2>1”(将文件描述2(标准错误输出)的内容重定向到文件描述符1(标准输出))(尼玛>符号竟然不支持搜索,害我搜搜不到,只能搜)
- JavaScript 工作原理之十一-渲染引擎及性能优化小技巧 1
- 「GIT SourceTree冲突」解决方案
- [ruby]devdocs windows setup
- 十、Spring的@Profile注解
- java比较吊的程序代码_java中 compareTo()的程序代码及用法
- 手把手教你实现机器学习SVM算法
- Unity3D基础16:网格过滤器和渲染器
- 怎么给PDF插入一个文本框写注释?PDF添加注释文本框教程
- 图能页:傻瓜式的手机网页制作服务
- 【高数】高数第七章节——微分方程概念一阶微分方程高阶微分方程
- Directory Opus一款功能强大的资源管理器
- java魂斗罗_魂斗罗java源代码分享
- MarkDown中使用gif的神器:LICEcap
- C++ map通过key获取value
- 将硬盘转换成GPT分区格式
- css实现标题左右横线
热门文章
- 基于用户画像 《列变行》 特征打标显示
- nginx-0.1.0文件分析3:ngx_send.c
- xp/win7,添加开机启动项
- Django 各类配置选项全集
- ASP.NET 状态管理概述(MSDN)
- 刷新aspx页面的六种方法
- shell 开机自动执行_windows还能这么玩?开机自动念情书
- 简单分析MySQL 一则慢日志监控误报问题
- 服务器开启虚拟机就死机,解决ESXi服务器上磁盘锁导致虚拟机卡死的问题
- linux 支持7代cpu型号,win7最高支持几代cpu