V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
Frankhong
V2EX  ›  Python

super().__init__()和 super(class,obj).__init__()有什么区别啊

  •  
  •   Frankhong · Dec 31, 2020 · 2380 views
    This topic created in 1950 days ago, the information mentioned may be changed or developed.
    class AttnDecoderRNN(nn.Module):
        def __init__(self, hidden_size, output_size, dropout_p=0.1, max_length=MAX_LENGTH):
            # super().__init__()
            super(AttnDecoderRNN, self).__init__()
    
    3 replies    2021-01-01 13:29:28 +08:00
    Trim21
        1
    Trim21  
       Dec 31, 2020 via Android
    实际没什么区别,不加参数的是比较新的写法
    hsfzxjy
        2
    hsfzxjy  
       Jan 1, 2021 via Android
    事实上你可以 super(nn.Module, self).__init__()直接调用祖父的函数
    yuruizhe
        3
    yuruizhe  
       Jan 1, 2021 via iPhone
    例子中是单继承,所以是一样的,如果是多继承,super(father_class,self)表示 self 的 MRO 的下一个父类,去调用下一个父类的初始化函数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2766 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    ♥ Do have faith in what you're doing.