python 新手遇到的問題

 

 

做爲新手,我把以前遇到的問題貼出來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']
相關文章
相關標籤/搜索