做为新手,我把以前遇到的问题贴出来python
错误提示1: TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)c++
1 class A: 2 def a(self): 3 print("I'm a") 4 5 A.a()
执行报错TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)app
表示没有对类进行实例, 改成:ide
1 class A: 2 def a(self): 3 print("I'm a") 4 obj=A() 5 obj.a()
或者:函数
1 class A: 2 @staticmethod # 静态方法 3 def a(): 4 print("I'm a") 5 6 A.a()
说明:ui
一是经过def定义的 普通的通常的,须要至少传递一个参数,通常用self,这样的方法必须经过一个类的实例去访问,相似于c++中经过对象去访问;this 二是在def前面加上@classmethod,这种类方法的一个特色就是能够经过类名去调用,可是也必须传递一个参数,通常用cls表示class,表示能够经过类直接调用;url 三是在def前面加上@staticmethod,这种类方法是静态的类方法,相似于c++的静态函数,他的一个特色是参数能够为空,一样支持类名和对象两种调用方式;spa |
在看一个例子:
1 class Person: 2 count = 0 3 def __init__(self): 4 self.name='Flay' 5 self.count+=1 6 Person.count +=2 7 print(self.count) 8 print(Person.count) 9 10 if __name__ == '__main__': 11 p = Person() 12 #Person.name
输出:
1 1 2 2
由于self 是类自己属性, Person.count 表示count为类的静态属性
若是直接Person.name 会直接报错:AttributeError: class Person has no attribute 'name'
错误提示2:RuntimeError: super-class __init__() of type ani was never called
1 # -*- coding:utf8 -*- 2 from PyQt4.QtGui import * 3 import sys 4 5 6 class ani(QWidget): 7 def __init__(self): 8 self.resize(10, 20) 9 10 11 if __name__=='__main__': 12 app = QApplication(sys.argv) 13 window = ani() 14 window.show() 15 sys.exit(app.exec_())
报错缘由:该类ani继承自QWidget,但没有给QWidget构造函数,若是类ani不继承任何基类能够这样:code
1 class ani(object): 2 def __init__(self): 3 print('aaa') 4 5 6 if __name__=='__main__': 7 win = ani()
因此,建立了一个名为 ani 的新类, 该类继承 QWidget 类。 所以咱们必须调用两个构造函数——ani 的构造函数和继承类 QWidget 类的构造函数,代码改成以下:
1 from PyQt4.QtGui import * 2 import sys 3 4 class ani(QWidget): 5 def __init__(self): 6 super(ani, self).__init__() 7 self.resize(10, 20) 8 9 10 if __name__=='__main__': 11 app = QApplication(sys.argv) 12 window = ani() 13 window.show() 14 sys.exit(app.exec_())
错误3:You are using pip version 7.1.2, however version 8.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' comm and.
pip install *** 报错
解决办法:
C:\Python35\Scripts>easy_install.exe pip==8.1.2
而后在安装包
C:\Python35>pip install C:\pypiwin32-219-cp35-none-win32.whl
错误4: Unable to find "/usr/include/python3.6m/pyconfig.h" when adding binary and data files.
使用pyinstaller.py 打包的时候提示找不到pyconfig.h 文件
解决办法; 安装python3-dev
sudo apt install python3-dev
本来/usr/include/python3.6m目录只有3个.h文件,安装python3-dev后该目录变为100多个.h文件,其中就包含pyconfig.h
问题5: pip修改镜像源
pip的镜像地址: https://pypi.org 由于地址在国外,pip install packages 慢的怀疑人生,或者出现timeout.此时能够考虑换成国内pypi镜像源
地址:https://pypi.doubanio.com/simple/
新建文件: ~./pip/pip.conf
填写index-url
[global] index-url = https://pypi.doubanio.com/simple/
注意一点,地址是https,负债会提示: The repository located at pypi.doubanio.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with '--trusted-host pypi.doubanio.com'.
问题6: 字符串(str)列表(list)元组(tuple) 互相转换
mystr='x1y1' mylist=['x2y2'] mytuple=('x3y3',) # ('x3y3') == <class 'str'>
相互转换
# 字符串 转 列表 Cmylist=[mystr] print(Cmylist) Cmylist=mystr.split('1') print(Cmylist) # 字符串 转 元组 CmyTuple=(mystr,) print(CmyTuple) # 列表 转 字符串 Cmystr="".join(mylist) #"@" @链接符 print(Cmystr) # 列表 转 元组 CmyTuple=tuple(mylist) print(CmyTuple) # 元组 转 列表 Cmylist=list(mytuple) print(Cmylist)
输出:
['x1y1'] ['x', 'y', ''] ('x1y1',) x2y2 ('x2y2',) ['x3y3']