Python学习笔记:2017/4/19---1

1.
IN PLACE的意思:
实例

l1 = range(0,8);
l2 = list(l1);
l3 = l2.reverse();
l4 = l2.sort();

这里l3和l4均无返回值,因为只是在l2自己发生了逆序和重新排序操作。

2.
元素复制,将会生成新的列表;列表变量名(指针理解)复制,不会生成新的列表

l1 = list(range(0,5));
l2 = l1; # 变量名复制
l1.append(100)

这个时候id(l1) == id(l2) 为True的,因为他们指向同一个内存
真正的复制元素有以下两种方法:

import copy

l2 = copy.deepcopy(l1);

l2 = l1[:]

此时生成的l2将会是一个新的list.

3.
 废话不多说直接祭上python3.3x的文档:(原文链接)

object.hash(self)

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. hash() should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

  可哈希对象是对象拥有hash(self)内置函数的对象。对于可哈希的对象执行这个函数将会返回一个整数。可哈希对象判断相等的唯一条件就是两者 的哈希值相等。如果我们的对象有多个属性值,我们会使用一种方法(比方说逻辑运算异或)来将其属性值结合在一起做比较。(如果不对,麻烦一定告诉我,谢谢!)

If a class does not define an eq() method it should not define a hash() operation either; if it defines eq() but not hash(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an eq() method, it should not implement hash(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

  如果一个类型定义没有定义eq()函数,那么它也不应该定义hash();因为如果它定义了eq而没有定义hash,那么它的实例在某个可哈希集合中将会无效(比方说在字典/集合这类类型中)。如果一个类型定义了一个可变对象而且定义了eq方法,那么它不应该去定义hash方法,因为在哈希集合中要求其中元素的哈希值是不变的(如果某个对象实例的哈希值发生了改变,那么他将会到错误的哈希表中。。)。

User-defined classes have eq() and hash() methods by default; with them, all objects compare unequal (except with themselves) and x.hash() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).

  用户定义的类中都有默认的eqhash方法;有了它,所有的对象实例都是不等的(除非是自己和自己比较),在做 x == y 比较时是和这个等价的 hash(x) == hash(y)。

A class that overrides eq() and does not define hash() will have its hash() implicitly set to None. When the hash() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable).

  这句话的意思是,如果我们定义了一个类型,该类型只定义了eq而没有定义hash的话,那么他的hash()会隐式的设置为None,如果某个情况下需要这个类型的哈希值,那么程序将会报错。具体请看下面代码:

>>> class A:
...     def __eq__(self, other):
...         return True
...     
>>> a = A()
>>> import collections
>>> isinstance(a, collections.Hashable)  # 发现它不是可哈希对象
False
>>> a.__hash__
>>> a.__hash__()
Traceback (most recent call last):
  File "<ipython-input-20-0fddd0562e01>", line 1, in <module>
    a.__hash__()
TypeError: 'NoneType' object is not callable

  然后往下看文档

If a class that overrides eq() needs to retain the implementation of hash() from a parent class, the interpreter must be told this explicitly by setting hash = .hash.

  如果某个重写了eq方法的对象需要保留hash属性,那么我们需要在类型设置中添加该语句 hash = .hash

请看代码

>>> class A:
...     __hash__ = object.__hash__
...     def __eq__(self, other):
...         return True
...     
>>> a = A()
>>> a.__hash__
<method-wrapper '__hash__' of A object at 0x7f21029cfa10>
>>> set([a,2,3])
{<__main__.A object at 0x7f21029cfa10>, 2, 3}

If a class that does not override eq() wishes to suppress hash support, it should include hash = None in the class definition. A class which defines its own hash() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections.Hashable) call.

  如果某个类型定义需要将hash属性删掉,那么我们可以在类变量中这样写 hash = None

  看完了还是有点小激动的,今天因为一个偶然原因,接触到了这么多的python知识。真的是相当高兴阿!

  然而我也发现eq hash这两个方法不能随意动,如果我么需要改写其中一个的话一定要仔细考虑可能的情况,以免出现问题。

4.
1.可哈希(hashable)和不可改变性(immutable)
如果一个对象在自己的生命周期中有一哈希值(hash value)是不可改变的,那么它就是可哈希的(hashable)的,因为这些数据结构内置了哈希值,每个可哈希的对象都内置了hash方法,所以可哈希的对象可以通过哈希值进行对比,也可以作为字典的键值和作为set函数的参数。所有Python中所有不可改变的的对象(imutable objects)都是可哈希的,比如字符串,元组,也就是说可改变的容器如字典,列表不可哈希(unhashable)。我们用户所定义的类的实例对象默认是可哈希的(hashable),它们都是唯一的,而hash值也就是它们的id()。
2. 哈希
它是一个将大体量数据转化为很小数据的过程,甚至可以仅仅是一个数字,以便我们可以用在固定的时间复杂度下查询它,所以,哈希对高效的算法和数据结构很重要。
3. 不可改变性
它指一些对象在被创建之后不会因为某些方式改变,特别是针对任何可以改变哈希对象的哈希值的方式
4. 联系:
因为哈希键一定是不可改变的,所以它们对应的哈希值也不改变。如果允许它们改变,,那么它们在数据结构如哈希表中的存储位置也会改变,因此会与哈希的概念违背,效率会大打折扣

5.
set内部一定是可迭代的对象

s0 = set(1,2,3);
s1 = set(1);

s2 = set([1,3,4]);

s0 跟 s1内部的对象都是不可迭代的因此也就会报错,s2调用的对象是列表,列表本身是一个迭代器,所以没问题,OK成功

6.
pyhon中 都是第一类,使用标识符命名的所有对象都有相同状态,因此能够命名的所有状态都可以当数据直接使用。
这意味着使用 命名的所有对象都有相同状态,能够都可以直接当数据直接处理。

7.
???后面会讲到:
匿名函数: lambda args: expression
生成器函数发送协议: yield x

8.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值