python
Holmes96
这个作者很懒,什么都没留下…
展开
-
使用@property
Python内置的@property装饰器就是负责把一个方法变成属性调用的。class Student(object): @property def score(self): return self._score @score.setter def score(self, value): if not isinstance(...转载 2019-12-04 22:05:11 · 90 阅读 · 0 评论 -
装饰器wraps
def a_new_decorator(a_func): def wrapTheFunction(): print("I am doing some boring work before executing a_func()") a_func() print("I am doing some boring work after ...转载 2019-12-02 23:04:53 · 130 阅读 · 0 评论 -
Python中的抽象方法@abstractmethod
抽象方法是父类的一个方法, 父类没有实现这个方法, 父类是不可以实例化的. 子类继承父类, 子类必须实现父类定义的抽象方法, 子类才可以被实例化. Python中的abc提供了@abstractmethod装饰器实现抽象方法的定义。抽象类的实现from abc import ABC, abstractmethodclass Foo(ABC): @abstractmethod...转载 2019-11-30 13:20:44 · 2249 阅读 · 0 评论 -
Difference between np.random.seed() and np.random.RandomState()
numpy.random.seed(0)resets the state of the existing globalRandomStateinstance that underlies the functions in thenumpy.randomnamespace.numpy.random.RandomState(0)returns a new seededRandomSt...转载 2019-11-27 16:54:35 · 231 阅读 · 0 评论 -
Is there a reason why people use super(class, self).__init__() instead of super().__init__()?
I suppose people use it to make the code compatible with Python2.x.Otherwise all Python2 users would have to add the class andselfto the super call.I’m not using Python3, but I think it doesn’t br...原创 2019-11-24 21:08:17 · 156 阅读 · 0 评论 -
为什么Python 3.6以后字典有序并且效率更高?
https://juejin.im/post/5d296e2af265da1bb31c6609原创 2019-11-21 20:01:08 · 149 阅读 · 0 评论 -
What is the meaning of a forward slash “/” in a Python method signature?
As explainedhere, the '/' as a parameter marks the end of parameters that are positional only (see here), i.e. parameters you can't use as keyword parameters. In the case of__eq__(self, value, /)th...原创 2019-11-19 12:02:47 · 121 阅读 · 0 评论 -
Bare asterisk in function arguments
Suppose you have function:def sum(a,key=5): return a + key You can call this function in 2 ways:sum(1,2)orsum(1,key=2)Suppose you want functionsumto be called only using keyword argum...原创 2019-11-19 11:59:57 · 63 阅读 · 0 评论 -
指定GPU训练
import osos.environ["CUDA_VISIBLE_DEVICES"] = "0" # RTX2080Ti######################################################import osos.environ["CUDA_VISIBLE_DEVICES"] = "1,2" # 2 *Tesla T4原创 2019-08-28 15:50:23 · 302 阅读 · 0 评论 -
在Jupyter Notebook里设置断点
安装ipdbpip install ipdb用法import ipdbdef fun(a): ipdb.set_trace() # 在这里开始调试 return do_some_thing_about(b)fun(a)快捷键n:下一个ENTER:重复上次命令q:退出p <变量>:打印变量c:继续l:查找当前在哪里s:进...转载 2019-07-30 15:20:06 · 4823 阅读 · 2 评论 -
python函数参数前面单星号(*)和双星号(**)
单星号(*):*agrs将所以参数以元组(tuple)的形式导入:>>> def foo(param1, *param2): print param1 print param2>>> foo(1,2,3,4,5)1(2, 3, 4, 5)双星号(**):**kwargs将参数以字典的形式导入>>...转载 2019-04-04 13:02:52 · 460 阅读 · 2 评论 -
python 中的[::-1]
a[i:j:s]s表示步进,缺省为1.所以a[i:j:1]相当于a[i:j]当s<0时,i缺省时,默认为-1; j缺省时,默认为-len(a)-1所以a[::-1]相当于 a[-1:-len(a)-1:-1],也就是从最后一个元素到第一个元素复制一遍。...原创 2019-03-20 18:35:16 · 117 阅读 · 0 评论 -
numpy中的tile函数
tile函数 numpy.tile(A,B)函数:重复A共B次,这里的B可以时int类型也可以是元组类型。>>> import numpy>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> nu...原创 2018-11-15 13:19:01 · 120 阅读 · 0 评论 -
numpy中的argsort()
argsort()argsort函数返回的是数组值从小到大的索引值Examples:One dimensional array:一维数组>>> x = np.array([3, 1, 2])>>> np.argsort(x)array([1, 2, 0])Two-dimensional array:二维数组>>>...原创 2018-11-15 13:30:01 · 5674 阅读 · 2 评论 -
Python字典get方法
描述Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值。语法get()方法语法:dict.get(key, default=None)参数key – 字典中要查找的键。default – 如果指定键的值不存在时,返回该默认值值。返回值返回指定键的值,如果值不在字典中返回默认值 None。实例import numpydict = {'Name...原创 2018-11-15 13:55:49 · 485 阅读 · 0 评论 -
Python中的sorted函数以及operator.itemgetter函数
operator.itemgetter()函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号。a = [1,2,3] >>> b=operator.itemgetter(1) //定义函数b,获取对象的第1个域的值>>> b(a) 2 >>> b...原创 2018-11-15 14:56:23 · 274 阅读 · 0 评论 -
Python set()
集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。创建格式:parame = {value01,value02,...}# 或者set(value)Examples:>>>basket = {'apple', '...原创 2018-11-21 16:08:30 · 230 阅读 · 0 评论 -
Python 列表推导式、字典推导式、集合推导式
推导式comprehensions(又称解析式),是Python的一种独有特性。推导式是可以从一个数据序列构建另一个新的数据序列的结构体。 共有三种推导:列表(list)推导式字典(dict)推导式集合(set)推导式一. 列表推导式1、使用[ ]生成list基本格式:variable = [out_exp_res for out_exp in input_list if ...原创 2018-11-21 16:59:16 · 130 阅读 · 0 评论 -
conda环境克隆和pip包的克隆
conda导出已有环境:conda env export > environment.yaml环境会被保存在 environment.yaml文件中。当我们想再次创建该环境,或根据别人提供的.yaml文件复现环境时,可以:conda env create -f environment.yaml就可以复现安装环境。移植过来的环境只是安装了你原来环境里用conda install等...原创 2018-12-26 23:58:30 · 3197 阅读 · 0 评论 -
将本地端口映射到公网,远程访问jupyter notebook
1.生成配置文件jupyter notebook --generate-config2.生成密码打开iPython,创建一个密文的密码:In [1]: from notebook.auth import passwdIn [2]: passwd()Enter password: Verify password: Out[2]: 'sha1:********************...原创 2018-12-29 00:08:26 · 3704 阅读 · 1 评论 -
python 打开绝对路径写法
("E:/machinelearninginaction/Ch02/digits/testDigits/0_13.txt")原创 2018-09-04 11:10:43 · 2224 阅读 · 0 评论