小编Mys*_*rio的帖子

Python 3.7数据类中的类继承

我目前正在尝试使用Python 3.7中引入的新数据类结构.我目前坚持尝试做一些父类的继承.看起来参数的顺序是由我当前的方法拙劣的,这样子类中的bool参数在其他参数之前传递.这导致类型错误.

from dataclasses import dataclass

@dataclass
class Parent:
    name: str
    age: int
    ugly: bool = False

    def print_name(self):
        print(self.name)

    def print_age(self):
        print(self.age)

    def print_id(self):
        print(f'The Name is {self.name} and {self.name} is {self.age} year old')

@dataclass
class Child(Parent):
    school: str
    ugly: bool = True


jack = Parent('jack snr', 32, ugly=True)
jack_son = Child('jack jnr', 12, school = 'havard', ugly=True)

jack.print_id()
jack_son.print_id()
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我得到了这个TypeError:

TypeError: non-default argument 'school' follows default argument
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

python python-3.x python-3.7 python-dataclasses

35
推荐指数
6
解决办法
7684
查看次数

无法在mac上安装miniconda3

我试图通过官方来源的bash脚本安装miniconda .我已经按照所有官方说明进行了双重检查,并通过bash安装了miniconda 3的现有信息.

我不断收到关于语言环境的错误;

tar: Failed to set default locale
tar: Failed to set default locale
installing: python-3.6.5-hc167b69_0 ...
tar: Failed to set default locale
Python 3.6.5 :: Anaconda, Inc.
installing: ca-certificates-2018.03.07-0 ...
tar: Failed to set default locale
installing: conda-env-2.6.0-h36134e3_0 ...
tar: Failed to set default locale
installing: libcxxabi-4.0.1-hebd6815_0 ...
tar: Failed to set default locale
installing: tk-8.6.7-h35a86e2_3 ...
tar: Failed to set default locale
installing: xz-5.2.3-h727817e_4 ...
tar: Failed to set default locale
installing: yaml-0.1.7-hc338f04_2 ... …
Run Code Online (Sandbox Code Playgroud)

bash sh anaconda miniconda

9
推荐指数
1
解决办法
355
查看次数

如何在python单元测试脚本中抑制ImportWarning

我目前正在运行一个单元测试脚本,它在控制台中成功传递了各种指定的测试,并带有一个唠叨的ImportWarning消息:

...../lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
  return f(*args, **kwds)
....
----------------------------------------------------------------------
Ran 7 tests in 1.950s

OK
Run Code Online (Sandbox Code Playgroud)

该脚本使用以下主函数运行:

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

我已经读过,当脚本被调用时,警告会被压抑:

python  -W ignore:ImportWarning -m unittest testscript.py
Run Code Online (Sandbox Code Playgroud)

但是,有没有办法在脚本本身中指定此忽略警告,以便-W ignore:ImportWarning每次运行testscript时都不必调用?

提前致谢.

python-import python-3.x python-unittest

8
推荐指数
2
解决办法
1913
查看次数