python 新手遇到的問題


 

 

作為新手,我把之前遇到的問題貼出來

錯誤提示1: TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

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)

表示沒有對類進行實例, 改為:

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()

 說明:

一是通過def定義的 普通的一般的,需要至少傳遞一個參數,一般用self,這樣的方法必須通過一個類的實例去訪問,類似於c++中通過對象去訪問;

二是在def前面加上@classmethod,這種類方法的一個特點就是可以通過類名去調用,但是也必須傳遞一個參數,一般用cls表示class,表示可以通過類直接調用;

三是在def前面加上@staticmethod,這種類方法是靜態的類方法,類似於c++的靜態函數,他的一個特點是參數可以為空,同樣支持類名和對象兩種調用方式;

在看一個例子:
 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不繼承任何基類可以這樣:

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']

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM