小编Mon*_*eal的帖子

从子类获取父私有或受保护的值

好吧,我有几乎相同的问题,除了一个细节:我需要获取基类的私有值。代码:

class Parent(object):
    def __init__(self):
        self.__field = 13    

class Child(Parent):
    """docstring for Child"""
    def __init__(self):
        super(Child, self).__init__()

    def ChildMethodWhichUsingParentField(self):
        return self.__field

if __name__ == '__main__':
    c = Child()
    c.ChildMethodWhichUsingParentField()
Run Code Online (Sandbox Code Playgroud)

口译输出:

Traceback (most recent call last):
  File "foo.py", line 20, in <module>
    c.ChildMethodWhichUsingParentField()
  File "foo.py", line 16, in ChildMethodWhichUsingParentField
    return self.__field
AttributeError: 'Child' object has no attribute '_Child__field'
Run Code Online (Sandbox Code Playgroud)

问题是口译员试图_Child__field在我需要时获取_Parent__field. 我可以使用 获得这个值@property,但它会阻止封装。我也可以解决这个问题,self._Parent__field但这是丑陋的,显然是糟糕的代码。还有其他方法吗?

python oop inheritance

6
推荐指数
2
解决办法
6290
查看次数

点子升级后无法安装numpy

出于某种原因,在pip升级到版本19.0之后,我无法安装最新版本的numpy(它仍然完美适用于pip版本18.1).

我跑的时候

pip install numpy --no-cache
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外

Exception:
Traceback (most recent call last):
  File "/home/me/.venvs/_/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 176, in main
    status = self.run(options, args)
  File "/home/me/.venvs/_/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 346, in run
    session=session, autobuilding=True
  File "/home/me/.venvs/_/lib/python3.6/site-packages/pip/_internal/wheel.py", line 848, in build
    assert building_is_possible
AssertionError
Run Code Online (Sandbox Code Playgroud)

我也使用python版本3.6和virtualenvwrapper版本16.0.0.

UPD 0

pip install numpy --no-cache -v
Run Code Online (Sandbox Code Playgroud)

给出了这个巨大的输出

UPD 1 我已经在numpy问题跟踪器上打开了一个错误请求.

python numpy pip python-3.x

6
推荐指数
1
解决办法
483
查看次数

如何在python中注释需要可变长度元组的函数?

我有一个函数,将不同长度的元组作为参数:

from typing import Tuple


def process_tuple(t: Tuple[str]):
    # Do nasty tuple stuff

process_tuple(("a",))
process_tuple(("a", "b"))
process_tuple(("a", "b", "c"))

Run Code Online (Sandbox Code Playgroud)

当我注释上述函数时,出现这些错误消息

fool.py:9: error: Argument 1 to "process_tuple" has incompatible type "Tuple[str, str]"; expected "Tuple[str]"
fool.py:10: error: Argument 1 to "process_tuple" has incompatible type "Tuple[str, str, str]"; expected "Tuple[str]"
Run Code Online (Sandbox Code Playgroud)

process_tuple真正适用于元组,我将它们用作可变长度的不可变列表。我尚未在互联网上找到关于此主题的任何共识,因此我想知道该如何注释这种输入。

python type-hinting python-3.x mypy

4
推荐指数
3
解决办法
835
查看次数

通过不同的execute_script调用创建和访问js变量

在我的测试运行期间,有一堆 js 脚本创建了我稍后必须访问的全局常量。目前代码库的构建方式是我无法避免的。基本上发生了接下来的事情:打开一个页面,在一个调用中执行一个脚本,在另一个调用中执行另一个脚本。

from selenium import webdriver


with webdriver.Firefox() as driver:
    driver.get("http://127.0.0.1:8000")
    driver.execute_script("const x = 1;")
    driver.execute_script("console.log(x + 1);")
Run Code Online (Sandbox Code Playgroud)

一切都因这个错误而崩溃。

Traceback (most recent call last):
  File "test_hello_selenium.py", line 24, in <module>
    driver.execute_script("console.log(x += 1);")
  File "~/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script
    'args': converted_args})['value']
  File "~/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "~/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: ReferenceError: x is not defined
Run Code Online (Sandbox Code Playgroud)

其实,司机并不重要。使用 chromedriver 错误保持不变。

实际问题是如何在不将两个 js 脚本合并为一个的情况下获得正确的结果,或者理解为什么这是不可能的。

python selenium selenium-webdriver

2
推荐指数
1
解决办法
540
查看次数