作为新手,我把之前遇到的问题贴出来

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

 class A:
def a(self):
print("I'm a") A.a()

执行报错TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

表示没有对类进行实例, 改为:

 class A:
def a(self):
print("I'm a")
obj=A()
obj.a()

或者:

 class A:
@staticmethod # 静态方法
def a():
print("I'm a") A.a()

说明:

一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问,类似于c++中通过对象去访问;

二是在def前面加上@classmethod,这种类方法的一个特点就是可以通过类名去调用,但是也必须传递一个参数,一般用cls表示class,表示可以通过类直接调用;

三是在def前面加上@staticmethod,这种类方法是静态的类方法,类似于c++的静态函数,他的一个特点是参数可以为空,同样支持类名和对象两种调用方式;

在看一个例子:
 class Person:
count = 0
def __init__(self):
self.name='Flay'
self.count+=1
Person.count +=2
print(self.count)
print(Person.count) if __name__ == '__main__':
p = Person()
#Person.name
输出:
  1
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
 # -*- coding:utf8 -*-
from PyQt4.QtGui import *
import sys class ani(QWidget):
def __init__(self):
self.resize(10, 20) if __name__=='__main__':
app = QApplication(sys.argv)
window = ani()
window.show()
sys.exit(app.exec_())

报错原因:该类ani继承自QWidget,但没有给QWidget构造函数,如果类ani不继承任何基类可以这样:

 class ani(object):
def __init__(self):
print('aaa') if __name__=='__main__':
win = ani()

所以,创建了一个名为 ani 的新类, 该类继承 QWidget 类。 因此我们必须调用两个构造函数——ani 的构造函数和继承类 QWidget 类的构造函数,代码改为如下:

 from PyQt4.QtGui import *
import sys class ani(QWidget):
def __init__(self):
super(ani, self).__init__()
self.resize(10, 20) if __name__=='__main__':
app = QApplication(sys.argv)
window = ani()
window.show()
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.

然后在安装包

C:\Python35>pip install C:\pypiwin32--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('')
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']

python 新手遇到的问题的更多相关文章

  1. (转)Python新手写出漂亮的爬虫代码2——从json获取信息

    https://blog.csdn.net/weixin_36604953/article/details/78592943 Python新手写出漂亮的爬虫代码2——从json获取信息好久没有写关于爬 ...

  2. (转)Python新手写出漂亮的爬虫代码1——从html获取信息

    https://blog.csdn.net/weixin_36604953/article/details/78156605 Python新手写出漂亮的爬虫代码1初到大数据学习圈子的同学可能对爬虫都有 ...

  3. Python 新手常犯错误

    Python 新手常犯错误(第二部分) 转发自:http://blog.jobbole.com/43826/ 作用域 在这篇文章里,我们来关注作用域在Python被误用的地方.通常,当我们定义了一个全 ...

  4. python新手必躺的5大坑

    python新手必躺的5大坑 对于Python新手来说,写代码很少考虑代码的效率和简洁性,因此容易造成代码冗长.执行慢,这些都是需要改进的地方.本文是想通过几个案列给新手一点启发,怎样写python代 ...

  5. python新手上车001

    python新手上车001 一般建议: 1.下载:从https://www.python.org/downloads/windows/  python官网进行下载建议就从3.7.2开始吧(我从这个版本 ...

  6. Python新手学习raise用法

    当程序出现错误时,系统会自动引发异常.除此之外,Python也允许程序自行引发异常,自行引发异常使用 raise 语句来完成. 很多时候,系统是否要引发异常,可能需要根据应用的业务需求来决定,如果程序 ...

  7. [Python]新手写爬虫全过程(已完成)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  8. Python 新手常犯错误(第二部分)

    转发自:http://blog.jobbole.com/43826/ 在之前几个月里,我教一些不了解Python的孩子来慢慢熟悉这门语言.渐渐地,我发现了一些几乎所有Python初学者都会犯的错误,所 ...

  9. Python新手学习基础之初识python——与众不同2

    看完了Python的缩进,现在来看看Python的标识符.引号和注释. 标识符 关于Python的标识符,其实不是与众不同,只是有一定的规则. 标识符是编程时使用的名字.在Python中,标识符有几点 ...

随机推荐

  1. Sonar安装配置

    https://www.sonarqube.org/downloads/ 下载sonar.当前版本为6.2 解压压缩包,进行配置: 修改sonarqube-6.2\conf\sonar.propert ...

  2. 机器学习——支持向量机(SVM)之核函数(kernel)

    对于线性不可分的数据集,可以利用核函数(kernel)将数据转换成易于分类器理解的形式. 如下图,如果在x轴和y轴构成的坐标系中插入直线进行分类的话, 不能得到理想的结果,或许我们可以对圆中的数据进行 ...

  3. 关于ajax请求,在参数中添加时间戳的必要性

    之前做项目的时候,看到别人的前端ajax请求代码中,都会带有一个时间戳类型的参数,当时随便查了一下,是为了防止浏览器缓存的原因,所以也没有进行深究,每次写的时候也习惯性的带一个,最近新项目中,我发现好 ...

  4. SQL语句生成指定范围内随机数

    1.生成随机实型数据 create procedure awf_RandDouble @min dec(14,2), @max dec(14,2), @result dec(14,2) output ...

  5. 阿里云部署nodejs服务器(windows)

    花了大半个月做的网站终于要上线了,周围的同学们很多都在使用阿里云的服务器,我也入手了一台.考虑到自己不是很适应ubuntu的命令行界面,于是买了个windows的,上网搜了一下,似乎都是用linux来 ...

  6. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  7. HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application 错误

    解决方法链接:http://stackoverflow.com/questions/17709076/http-status-500-the-absolute-uri-http-java-sun-co ...

  8. python --enable-shared

    https://github.com/docker-library/python/issues/21 例如编译安装python3.5.2,脚本如下: wget https://s3.cn-north- ...

  9. js_继承

    一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 复制代码代码如下: <SPAN style="<SPAN style="FONT- ...

  10. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...