Python学习笔记(一)

来源:互联网 发布:手机淘宝联盟自己购买 编辑:程序博客网 时间:2024/05/01 06:48

001

Python装饰器的装饰顺序和执行顺序是相反的。装饰器的主要输入始终是一个需要被装饰的函数:
@decorator1
@decorator2
def add(…)
在装饰时先用d2装饰add,再用d1装饰d2装饰器返回后的函数,所以最后的函数入口实际上是d1装饰器返回的函数。

002

Python 只有str类型,没有char类型
Python的str连接中,用空格分开链接,比用加号连接要快,粗略测试要快一倍。

003

slice操作是浅拷贝
All slice operations return a new list containing the requested elements.

004

The execution of a function introduces a new symbol table used for the local variables of the function.
Global variables cannot be directly assigned a vaule within a function(unless named in a global statement), althought they may be referenced.[变量名如果不是通过函数参数传进来的就被视为本地临时变量,除非使用global进行声明绑定。]

005

Python中的函数总是有返回值的:就算没有return语句,也有返回值None.

006

python的函数默认参数可以使用变量进行绑定,但是该绑定仅发生在函数被解释器编译时,即只有一次。当默认参数绑定到mutable对象时,每次调用的默认参数可能是变化的,这个特性用的好会是魔法。

007

list的insert、remove、sort方法返回值为None,IDLE在返回值为None的时候什么也不打印,其他时候要么执行print等打印语句,要么打印返回值。

008

list comprehension可以使用map函数来做底层实现。两者原理都是一样的,对list中的每个元素都执行相同的语句块。

009

object class instance
scope & namespace
The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function.

010

You can conceivably place a class definition in a branch of an if statement, or inside a function.
[这句话真是有意思。如果把class的定义放在if True或if False之后就可以定义或者不定义一个类(False条件下不会支持类定义语句,并且定在if内的class居然是全局可用de);如果把类定义在func内部则仅为func内部的局部变量,不用return语句等返回,则外部无法获取相应实例。]
实际上,正是由于scope和namespace的规则导致了if and func之间差别,因为在做实验时if语句是放在global作用域中的,而func自成一个独立作用域。

011

myx = MyClass()
Instantiation过程实际上使用了方法的概念。MyClass作为被调用的方法,其返回值对象与名字myx进行了绑定。

012

执行一个程序只需要两个事情:
① 代码段执行入口;
② 必要的运行时参数.
所以,我们可以也应该支持方法的绑定与传递,毕竟方法入口也就是一个指针。

013

Python 类 name mangling是程序员可见的,这与C/C++大为不同。

014

Python的class实例对象由new()创建,其返回值为新创建的inst对象,then the new instance’s init() method will be invoked like init(self[,…]), where self is the new instance and the remaining arguments are the same as were passed to new().
Python的class是可调用的。可以将类对象视为创建实例的工厂函数。在类对象被调用时参数被传入new()中,并最后传入到 init(self[,…])。Instance对象也是可调用的,只要定义了相应的call方法。

015

Every object of python has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

016

Python的数字是面向数学域de.所以1与1.0是相等的,在加入到set的过程中只能有一个存在。

0 0