推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
daiqiangbudainiu
V2EX  ›  Python

继承的父类的方法,我想修改部分逻辑,有什么好的方法吗

  •  
  •   daiqiangbudainiu · Jan 29, 2019 · 3124 views
    This topic created in 2689 days ago, the information mentioned may be changed or developed.
    如题

    我自己的下载 pipeline,继承了 scrapy 的 FilesPipeline 类,我现在想修改 media_to_download 这个方法

    其实就是想加一点 log,逻辑上有一点小小的修改,大体上的逻辑还是 scrapy 中的逻辑

    我只会复制粘贴 FilesPipeline 中的方法代码过来,然后自己修改

    有没有什么比较好的方法能做到加一点 log 这个要求吗?
    Supplement 1  ·  Jan 29, 2019
    另外,有关于 filespipeline 的详细文档不?或者哪位大大的详细解析
    13 replies    2019-01-30 10:02:55 +08:00
    mainlong
        1
    mainlong  
       Jan 29, 2019 via Android
    装饰器?
    Faiz555
        2
    Faiz555  
       Jan 29, 2019
    super()?
    daiqiangbudainiu
        3
    daiqiangbudainiu  
    OP
       Jan 29, 2019
    @mainlong 没懂
    wwg1994
        4
    wwg1994  
       Jan 29, 2019
    装饰器不可行吧,不能修改原函数内部的逻辑,加日志倒是莫得问题。
    lecion
        5
    lecion  
       Jan 29, 2019 via Android
    @wwg1994 日志也只能前后插桩,看楼主的意思是想在过程中也加日志
    dendi009
        6
    dendi009  
       Jan 29, 2019
    只想加 log 用装饰器就行 @decorator 就能满足, 逻辑小改动 装饰器应该也能满足, 取决于改什么逻辑 。 实在不行继承父类, 重写类方法 最直接。
    wwg1994
        7
    wwg1994  
       Jan 29, 2019
    @lecion 那就莫得法了,ctrl+c,ctrl+v,重写吧😀
    pabupa
        8
    pabupa  
       Jan 29, 2019 via Android
    改他的源码呀
    pabupa
        9
    pabupa  
       Jan 29, 2019 via Android   ❤️ 1
    @pabupa 又不需要编译。
    daiqiangbudainiu
        10
    daiqiangbudainiu  
    OP
       Jan 29, 2019
    @dendi009 重写岂不是得复制粘贴代码之后,再修改,他的方法有很多 import,我懒得挨个 import 了
    smdbh
        11
    smdbh  
       Jan 29, 2019
    不可能。
    重写函数。
    zwzmzd
        12
    zwzmzd  
       Jan 29, 2019 via iPhone
    看一看 monkey patching ?主要原理就是把原有的函数对象动态换掉
    daiqiangbudainiu
        13
    daiqiangbudainiu  
    OP
       Jan 30, 2019
    @zwzmzd 学习了,我去看看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3152 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    ♥ Do have faith in what you're doing.