随笔分类 -  python

摘要:>>> class test(): def __init__(self): pass def __repr__(self): return '1sdf' >>> t = test() >>> t #不用print 直接打印出重构的值,面向程序,在程序中传递 1sdf >>> class test(Exception): def __i... 阅读全文
posted @ 2018-02-28 16:47 我是外婆 阅读(135) 评论(0) 推荐(0)
摘要:#自定义异常 需要继承Exception class MyException(Exception): def __init__(self, *args): self.args = args #raise MyException('爆出异常吧哈哈') #常见做法定义异常基类,然后在派生不同类型的异常 class loginError(MyException): ... 阅读全文
posted @ 2018-01-11 17:09 我是外婆 阅读(12767) 评论(0) 推荐(0)
摘要:# _*_coding:utf-8_*_ # author:leo # date: # email:alplf123@163.com # python中的三目运算 # 常见的方式 is True ? val1:val2 # python中 a = 4 print(10 if a > 3 else 5) # 判断条件挪到if后面 满足条件返回前面的值,否则返回后面的值 #在生成式中的应... 阅读全文
posted @ 2018-01-10 00:04 我是外婆 阅读(208) 评论(0) 推荐(0)
摘要:# _*_coding:utf-8_*_ # author:leo # date: # email:alplf123@163.com from collections import Iterable, Iterator class myIterator(): _data = None _count = 0 def __init__(self, data): ... 阅读全文
posted @ 2018-01-06 20:41 我是外婆 阅读(493) 评论(0) 推荐(0)
摘要:#coding:utf-8 import os import sys #当前环境py2.7 print(sys.getdefaultencoding()) #注意是编码方式,不是编码 #定义一个字符串 str = '中' #print(str) print(len(str)) #长度3, utf-8 #str.encode('gbk') #报错,当前编码环境为ascii 不能直接从ascii映射... 阅读全文
posted @ 2018-01-05 11:05 我是外婆 阅读(433) 评论(0) 推荐(0)
摘要:class Rules(): __spe__ = "special"#特殊变量 __private = 'private' #完全私有子类无法访问 _halfprivate = 'halfprivate' #只能在类中模块中使用 def _halfprivatefunc(self): print('half func') def __p... 阅读全文
posted @ 2018-01-03 10:31 我是外婆 阅读(125) 评论(0) 推荐(0)
摘要:# 多个重载 # 可以为string a = '我爱中国' b = bytes(a, encoding='gbk') print(b) # 可以为迭代对象 c = [1, 2, 3, 4] d = bytes(c) print(d) # 同时支持b e = b'sdfsdf' print(bytes(b)) #直接输出十六进制文本串,很方便 print(bytes(a, encoding='gb... 阅读全文
posted @ 2018-01-02 17:39 我是外婆 阅读(166) 评论(0) 推荐(0)
摘要:#code:utff-8 import sys from functools import reduce # map #允许接受两个参数,第一个为函数,或者为函数表达式,第二个参数为可迭代对象 # 返回list def test(x): return x * 2 t = [1, 2, 3, 4] for tmp in map(test, t): print(tmp) # filt... 阅读全文
posted @ 2017-12-29 11:17 我是外婆 阅读(98) 评论(0) 推荐(0)
摘要:# code:utf-8 #导入 ABCMeta abstractmethodd from abc import ABCMeta, abstractmethod, abstractproperty class Parent(metaclass=ABCMeta): #2.+ 中以次此种方法 #Python 中没有接口的概念 __metaclass__ = ABCMeta ... 阅读全文
posted @ 2017-12-29 10:16 我是外婆 阅读(236) 评论(0) 推荐(0)