super

Python 3 super and metaprogramming

China☆狼群 提交于 2021-01-27 16:11:08
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I

Python 3 super and metaprogramming

喜夏-厌秋 提交于 2021-01-27 15:45:57
问题 I'm trying to duplicate and then modify a class programmatically but I'm running into problems with python 3's magic super for example the following class Base(): def __init__(self): print("Base init") class Orginal(Base): def __init__(self): super().__init__() print("Orginal init") Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__)) Modified.f = lambda self: print("f") m = Modified() raises TypeError: super(type, obj): obj must be an instance or subtype of type So I

Calling super's forward() method

戏子无情 提交于 2020-12-30 05:47:03
问题 What is the most appropriate way to call the forward() method of a parent Module ? For example, if I subclass the nn.Linear module, I might do the following class LinearWithOtherStuff(nn.Linear): def forward(self, x): y = super(Linear, self).forward(x) z = do_other_stuff(y) return z However, the docs say not to call the forward() method directly: Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since

Calling super's forward() method

允我心安 提交于 2020-12-30 05:45:01
问题 What is the most appropriate way to call the forward() method of a parent Module ? For example, if I subclass the nn.Linear module, I might do the following class LinearWithOtherStuff(nn.Linear): def forward(self, x): y = super(Linear, self).forward(x) z = do_other_stuff(y) return z However, the docs say not to call the forward() method directly: Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since

Calling super's forward() method

与世无争的帅哥 提交于 2020-12-30 05:44:49
问题 What is the most appropriate way to call the forward() method of a parent Module ? For example, if I subclass the nn.Linear module, I might do the following class LinearWithOtherStuff(nn.Linear): def forward(self, x): y = super(Linear, self).forward(x) z = do_other_stuff(y) return z However, the docs say not to call the forward() method directly: Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since

Calling super's forward() method

孤街醉人 提交于 2020-12-30 05:43:59
问题 What is the most appropriate way to call the forward() method of a parent Module ? For example, if I subclass the nn.Linear module, I might do the following class LinearWithOtherStuff(nn.Linear): def forward(self, x): y = super(Linear, self).forward(x) z = do_other_stuff(y) return z However, the docs say not to call the forward() method directly: Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since