zoukankan      html  css  js  c++  java
  • 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:Python35Scripts>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']
  • 相关阅读:
    Python3 字典Dict(十三)
    Python3 元组Tuple(十二)
    Python3 列表List(十一)
    Python3 循环语句(十)
    Python3 条件控制(九)
    Python3 运算符(八)
    Swift3.0语法2
    Swift反射机制实现 AppDelegate 字符串获取类并成为根控制器
    Swift语法(更新)
    单例
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4260732.html
Copyright © 2011-2022 走看看