Home Backend Development Python Tutorial python黑魔法之参数传递

python黑魔法之参数传递

Jun 10, 2016 pm 03:06 PM
python Parameter passing

我们都听说,python世界里面,万物皆对象。
怎么说万物皆对象呢?最常见的:

> class A: pass
> a = A()
Copy after login

我们说a是一个对象。
那么既然是万物了,其实A也是对象。3 也是对象。True 也是对象。"hello" 也是对象。
> def Func(): pass
o~yee, Func 也是对象。
那么对象之间的传递是如何呢?我们看看下面两个简单的例子:

> a = 3
> b = a
> b = 3 + 1
> print b
4
> print a
3

> a = []
> b = a
> b.append(1)

> print a
[1]
> print b
[1]

Copy after login

不是都说python所有对象都是引用传递吗?为毛第一个b不是3?
好吧。事实是,在python的实现上,对象分为mutable 和 immutable。
这里说的对象分类,是说在实现上具备这样的特性。而非对象本身的属性。
什么是immutable?表示对象本身不可改变。这里先记住一点,是对象 本身 不可改变。
什么叫做对象本身不可改变呢?
一个简单的例子:

> a = (1,2,3)
> a[0] = 10  
Copy after login

TypeError: 'tuple' object does not support item assignment
元组的元素在初始化后就不能再被改变。也就是说,元组对象具备immutable的特性。
那么很简单,相对的,mutable 就是可变的。比如:

> a = {}
> a[0] = 10
Copy after login

有了上面的两个例子,相信大家已经有了基本的认识。
那么,在python世界中,哪些是具备immutable特性,哪些又是mutable的呢?
简单讲,基本类型都是immutable, 而object都是mutable的。
比如说:int, float, bool, tuple 都是immutable。
再比如:dict, set, list, classinstance 都是mutable的。
那么问题来了。既然说基本类型是 immutable ,那么最上面的 b = 3 + 1 为什么不会像tuple一样,抛异常呢?
原因在于,int 对+操作会执行自己的__add__方法。而__add__方法会返回一个新的对象。
事实是,当基本类型被改变时,并不是改变其自身,而是创建了一个新的对象。最终返回的是新的对象的引用。
怎么证明?
我们可以使用一个叫做id()的函数。该函数会返回对象的一个唯一id(目前的实现可以间接理解为对象的内存地址)。
那么我们看下:

> a = 3
> id(a)
140248135804168

> id(3)
140248135804168

> id(4)
140248135804144

> a = a + 1
> id(a)
140248135804144

Copy after login

you see ? 当我们执行a=a+1 后,id(a) 已经改变了。
深究一点,为什么会这样呢?
其实,a = a + 1 经历了两个过程:

  • 1、a + 1
  • 2、a 赋值

第2步只是一个引用的改变。重点在第1步。a + 1,那么python实际上会调用a.__add__(1)。
对于int类型__add__函数的实现逻辑,是创建了一个新的int对象,并返回。
不知道细心的你有没有发现一个特别的地方?
id(4)的值等于id(3+1) 。这个只是python对int,和bool做的特殊优化。不要以为其他基本类型只要值一样都会指向相同的对象。
有个特殊的例子,str。做个简单的实验:

> a = "hello"
> id(a)
4365413232
> b = "hell"
> id(b)
4365386208

> id(a[:-1])
4365410928
> id(a[:-1])
4365413760

Copy after login

看到了吗?虽然值相同,但是还是指向(创建)了不同的对象,尤其是最后两句,哪怕执行相同的操作,依然创建了不同的对象。
python这么傻,每次都创建新的对象?
no no no 他只是缓存了“一些”结果。我们可以再试试看:

> a = "hello"
> ret = set()
> for i in range(1000):
  ret.add(id(a[:-1]))
> print ret
{4388133312, 4388204640}
Copy after login

看到了吗?python还是挺聪明的。不过具体的缓存机制我没有深究过,期望有同学能分享下。
再次回到我们的主题,python中参数是如何传递的?
答案是,引用传递。
平时使用静态语言的同学(比如我),可能会用下面的例子挑战我了:

def fun(data):
  data = 3

a = 100
func(a)

print a # 100

Copy after login

不是尼玛引用传递吗?为毛在执行func(a)后,a 的值没有改变呢?这里犯了一个动态语言基本的错误。
data=3,语义上是动态语言的赋值语句。千万不要和C++之类的语言一个理解。
看看我们传入一个mutable 的对象:

> def func(m):
  m[3] = 100

> a = {}
> print a
{}
> func(a)
> print a
{3:100}

Copy after login

现在同学们知道该如何进行参数传递了吧?好嘞,进阶!
像很多语言如C++,js,swift... 一样,python 的函数声明支持默认参数:
def func(a=[]): pass
不知道什么意思?自己看书去!
我这里要说的是,如果我们的默认参数是mutable类型的对象,会有什么黑魔法产产生?
我们看看下面的函数:

def func(a=[]):
  a.append(3)
  return a
Copy after login

可能有同学会说了:我去!这么简单?来骗代码的吧?
但是,真的这么简单吗?我们看下下面的调用结果:

> print func()
[3]
> print func()
[3,3]
> print func()
[3,3,3]
Copy after login

这真的是你想要的结果吗?
No,我要的是[3],[3],[3]!
原因?好吧,我们再用下id()神奇看看:

def func(a=[]):
  print id(a)
  a.append(3)
  return a

> print func()
4365426272
[3]
> print func()
4365426272
[3, 3]
> print func()
4365426272
[3, 3, 3]

Copy after login

明白没?原来在python中,*默认参数不是每次执行时都创建的!*
这下你再想想,曾经嘲笑过的代码(至少我)为什么要 多此一举:

def func(a=None):
  if a is None:
    a = []
Copy after login

这里在顺带提一下==, is:
== : 值比较
is : 比较左右两边是否是同一个对象。 a is b ==> id(a) == id(b)
ok, let's move on!
我们都知道,在python中,不定参数我们可以这样定义:
def func(*args, **kv): pass
什么你不知道?看书去!
那args和kv到底是什么情况呢?到底是mutable 还是 immutable 呢?
再一次请出id()神器:

def func(*args):
  print id(args)


> a = [1,2]
> print id(a)
4364874816
> func(*a)
4364698832
> func(*a)
4364701496
Copy after login

看到了吧?实际上args也会产生一个新的对象。但是值是填入的传入参数。那么每一个item也会复制吗?
我们再看看:

def func(*args):
  print id(args[0])

> a = [1,2]
> print id(a[0])
140248135804216
> func(*a)
140248135804216

Copy after login

答案是,No。值会像普通list赋值一样,指向原先list(a)所引用的对象。
那么为什么会这样呢?
python的源码就是这么写的.......
最最后,还记得我说过的一句话吗?
immutable 限制的是对象本身不可变
意思就是说,对象的immtable 只是限制自身的属性能否被改变,而不会影响到其引用的对象。
看下下面的例子:

> a = [1,2]
> b = (a,3)
> b[1] = 100
TypeError: 'tuple' object does not support item assignment

> print b
([1, 2], 3)
> b[0][0] = 10
> print b
([10, 2], 3)

Copy after login

最最最后,我有个对象,它本身应该是 mutable 的,但是我想让他具备类似immutable的特性,可以吗?
答案是,可以模拟!
还是之前说的,immutable 限制的是其自身属性不能改变。
那么,我们的可以通过重定义(重载)属性改变函数,来模拟immutable特性。
python可以吗?O~Yee
在python的类函数中,有这样的两个函数: __setattr__ 和 __delattr__。分别会在对象属性赋值和删除时执行。
那么我们可以进行简单重载来模拟immutable:

class A:
  def __setattr__(self, name, val):
    raise TypeError("immutable object could not set attr")
Copy after login

以上就是为大家介绍的python黑魔法,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does PS feathering control the softness of the transition? How does PS feathering control the softness of the transition? Apr 06, 2025 pm 07:33 PM

The key to feather control is to understand its gradual nature. PS itself does not provide the option to directly control the gradient curve, but you can flexibly adjust the radius and gradient softness by multiple feathering, matching masks, and fine selections to achieve a natural transition effect.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to set up PS feathering? How to set up PS feathering? Apr 06, 2025 pm 07:36 PM

PS feathering is an image edge blur effect, which is achieved by weighted average of pixels in the edge area. Setting the feather radius can control the degree of blur, and the larger the value, the more blurred it is. Flexible adjustment of the radius can optimize the effect according to images and needs. For example, using a smaller radius to maintain details when processing character photos, and using a larger radius to create a hazy feeling when processing art works. However, it should be noted that too large the radius can easily lose edge details, and too small the effect will not be obvious. The feathering effect is affected by the image resolution and needs to be adjusted according to image understanding and effect grasp.

What impact does PS feathering have on image quality? What impact does PS feathering have on image quality? Apr 06, 2025 pm 07:21 PM

PS feathering can lead to loss of image details, reduced color saturation and increased noise. To reduce the impact, it is recommended to use a smaller feather radius, copy the layer and then feather, and carefully compare the image quality before and after feathering. In addition, feathering is not suitable for all cases, and sometimes tools such as masks are more suitable for handling image edges.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL installation error solution MySQL installation error solution Apr 08, 2025 am 10:48 AM

Common reasons and solutions for MySQL installation failure: 1. Incorrect username or password, or the MySQL service is not started, you need to check the username and password and start the service; 2. Port conflicts, you need to change the MySQL listening port or close the program that occupies port 3306; 3. The dependency library is missing, you need to use the system package manager to install the necessary dependency library; 4. Insufficient permissions, you need to use sudo or administrator rights to run the installer; 5. Incorrect configuration file, you need to check the my.cnf configuration file to ensure the configuration is correct. Only by working steadily and carefully checking can MySQL be installed smoothly.

See all articles