In Python2 can I invoke super in __init__ (+ other methods) without params so that the super method is invoked w the same params as the given method?Super keyword in RubyUnderstanding Python super() with __init__() methodsWhat does Ruby have that Python doesn't, and vice versa?Why aren't superclass __init__ methods automatically invoked?Preventing a class from direct instantiation in PythonInitializing field outside __init__Determine whether super().__new__ will be object.__new__ in Python 3?Python 3: super() raises TypeError unexpectedlyPython class object creation in inheritanceConstructor failing in inherited class pythonMultiple Inheritance and calling super()

Getting a W on your transcript for grad school applications

How can I get a job without pushing my family's income into a higher tax bracket?

As matter approaches a black hole, does it speed up?

What does this colon mean? It is not labeling, it is not ternary operator

Would Hubble Space Telescope improve black hole image observed by EHT if it joined array of telesopes?

How wide is a neg symbol, how to get the width for alignment?

Why do only some White Walkers shatter into ice chips?

Independent, post-Brexit Scotland - would there be a hard border with England?

What to use instead of cling film to wrap pastry

Purpose of のは in this sentence?

Why is the relative clause in the following sentence not directly after the noun and why is the verb not in the end of the sentence?

A mathematically illogical argument in the derivation of Hamilton's equation in Goldstein

Manager is threatening to grade me poorly if I don't complete the project

What is the most remote airport from the center of the city it supposedly serves?

In Avengers 1, why does Thanos need Loki?

Position of past participle and extent of the Verbklammer

How do LIGO and VIRGO know that a gravitational wave has its origin in a neutron star or a black hole?

what to look for in luxury cars like Acura/Lexus

A non-Borel union of unit half-open squares

How long would it take for people to notice a mass disappearance?

How to model the curly cable part of the phone

Why do money exchangers give different rates to different bills?

Can my company stop me from working overtime?

Are there any Final Fantasy Spirits in Super Smash Bros Ultimate?



In Python2 can I invoke super in __init__ (+ other methods) without params so that the super method is invoked w the same params as the given method?


Super keyword in RubyUnderstanding Python super() with __init__() methodsWhat does Ruby have that Python doesn't, and vice versa?Why aren't superclass __init__ methods automatically invoked?Preventing a class from direct instantiation in PythonInitializing field outside __init__Determine whether super().__new__ will be object.__new__ in Python 3?Python 3: super() raises TypeError unexpectedlyPython class object creation in inheritanceConstructor failing in inherited class pythonMultiple Inheritance and calling super()






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am programming in Python 2.7. In other languages like Ruby, there is an invocation of super in a method such that one does not need to specify the parameters with which to call super -- super. One omits the parameters in one's super call and the given super method is invoked with the same parameters as one's given method was called.



Here is an example from the StackOverflow question Super keyword in Ruby:



class Foo
def baz(str)
p 'parent with ' + str
end
end

class Bar < Foo
def baz(str)
super
p 'child with ' + str
end
end

Bar.new.baz('test') # => 'parent with test' 'child with test'


As you can see the method baz in the class Bar contains the invocation of super without any parameters and the super method baz in the super class Foo implicitly gets called with the same parameter(s) as the method baz in the child class Bar



For a variety of reasons this is very convenient, and while obviously implicit behavior in programming has its drawbacks, it is really useful to be able to have an __init__ method in a child class that does stuff in that child class and then invokes __init__ via super without having to know the parameters for __init__ in the super class.



Please help, any advice would be useful. I have been searching for an answer and unable to find one. I have seen many questions and answers on StackOverflow about super and super in __init__ and super in Python, but nonw about whether there is syntax for calling super that does not require parameters to be specified and calls it with the same parameters as the given method was called in the child class.



What I am trying to do is very similar to raise in a try, except, raise construct in Python:



try:
linux_interaction()
except Exception as error:
logger.critical(str(error))
raise


In the above block I catch all exceptions, I log any exception as critical and then I throw it with just a call to raise, no parameters. That calling syntax of raise with no parameters causes the exception that was caught in the variable named error to be thrown and preserves the calling stack/stack trace. If one called raise error instead of just raise, then the stack trace from where the error originated would be lost.



I am trying to do something similar here, just invoke super and get the super method invoked with the same parameters as the given method without needing to know what they are and without needing to specify them -- that would be convenient, right?



Anyone know if this is possible and if so what the syntax in Python 2.7 would be?










share|improve this question

















  • 2





    No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

    – Martijn Pieters
    Mar 22 at 22:30






  • 2





    Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

    – o11c
    Mar 22 at 22:30






  • 2





    The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

    – chepner
    Mar 22 at 22:30







  • 2





    I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

    – o11c
    Mar 22 at 22:55

















1















I am programming in Python 2.7. In other languages like Ruby, there is an invocation of super in a method such that one does not need to specify the parameters with which to call super -- super. One omits the parameters in one's super call and the given super method is invoked with the same parameters as one's given method was called.



Here is an example from the StackOverflow question Super keyword in Ruby:



class Foo
def baz(str)
p 'parent with ' + str
end
end

class Bar < Foo
def baz(str)
super
p 'child with ' + str
end
end

Bar.new.baz('test') # => 'parent with test' 'child with test'


As you can see the method baz in the class Bar contains the invocation of super without any parameters and the super method baz in the super class Foo implicitly gets called with the same parameter(s) as the method baz in the child class Bar



For a variety of reasons this is very convenient, and while obviously implicit behavior in programming has its drawbacks, it is really useful to be able to have an __init__ method in a child class that does stuff in that child class and then invokes __init__ via super without having to know the parameters for __init__ in the super class.



Please help, any advice would be useful. I have been searching for an answer and unable to find one. I have seen many questions and answers on StackOverflow about super and super in __init__ and super in Python, but nonw about whether there is syntax for calling super that does not require parameters to be specified and calls it with the same parameters as the given method was called in the child class.



What I am trying to do is very similar to raise in a try, except, raise construct in Python:



try:
linux_interaction()
except Exception as error:
logger.critical(str(error))
raise


In the above block I catch all exceptions, I log any exception as critical and then I throw it with just a call to raise, no parameters. That calling syntax of raise with no parameters causes the exception that was caught in the variable named error to be thrown and preserves the calling stack/stack trace. If one called raise error instead of just raise, then the stack trace from where the error originated would be lost.



I am trying to do something similar here, just invoke super and get the super method invoked with the same parameters as the given method without needing to know what they are and without needing to specify them -- that would be convenient, right?



Anyone know if this is possible and if so what the syntax in Python 2.7 would be?










share|improve this question

















  • 2





    No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

    – Martijn Pieters
    Mar 22 at 22:30






  • 2





    Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

    – o11c
    Mar 22 at 22:30






  • 2





    The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

    – chepner
    Mar 22 at 22:30







  • 2





    I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

    – o11c
    Mar 22 at 22:55













1












1








1








I am programming in Python 2.7. In other languages like Ruby, there is an invocation of super in a method such that one does not need to specify the parameters with which to call super -- super. One omits the parameters in one's super call and the given super method is invoked with the same parameters as one's given method was called.



Here is an example from the StackOverflow question Super keyword in Ruby:



class Foo
def baz(str)
p 'parent with ' + str
end
end

class Bar < Foo
def baz(str)
super
p 'child with ' + str
end
end

Bar.new.baz('test') # => 'parent with test' 'child with test'


As you can see the method baz in the class Bar contains the invocation of super without any parameters and the super method baz in the super class Foo implicitly gets called with the same parameter(s) as the method baz in the child class Bar



For a variety of reasons this is very convenient, and while obviously implicit behavior in programming has its drawbacks, it is really useful to be able to have an __init__ method in a child class that does stuff in that child class and then invokes __init__ via super without having to know the parameters for __init__ in the super class.



Please help, any advice would be useful. I have been searching for an answer and unable to find one. I have seen many questions and answers on StackOverflow about super and super in __init__ and super in Python, but nonw about whether there is syntax for calling super that does not require parameters to be specified and calls it with the same parameters as the given method was called in the child class.



What I am trying to do is very similar to raise in a try, except, raise construct in Python:



try:
linux_interaction()
except Exception as error:
logger.critical(str(error))
raise


In the above block I catch all exceptions, I log any exception as critical and then I throw it with just a call to raise, no parameters. That calling syntax of raise with no parameters causes the exception that was caught in the variable named error to be thrown and preserves the calling stack/stack trace. If one called raise error instead of just raise, then the stack trace from where the error originated would be lost.



I am trying to do something similar here, just invoke super and get the super method invoked with the same parameters as the given method without needing to know what they are and without needing to specify them -- that would be convenient, right?



Anyone know if this is possible and if so what the syntax in Python 2.7 would be?










share|improve this question














I am programming in Python 2.7. In other languages like Ruby, there is an invocation of super in a method such that one does not need to specify the parameters with which to call super -- super. One omits the parameters in one's super call and the given super method is invoked with the same parameters as one's given method was called.



Here is an example from the StackOverflow question Super keyword in Ruby:



class Foo
def baz(str)
p 'parent with ' + str
end
end

class Bar < Foo
def baz(str)
super
p 'child with ' + str
end
end

Bar.new.baz('test') # => 'parent with test' 'child with test'


As you can see the method baz in the class Bar contains the invocation of super without any parameters and the super method baz in the super class Foo implicitly gets called with the same parameter(s) as the method baz in the child class Bar



For a variety of reasons this is very convenient, and while obviously implicit behavior in programming has its drawbacks, it is really useful to be able to have an __init__ method in a child class that does stuff in that child class and then invokes __init__ via super without having to know the parameters for __init__ in the super class.



Please help, any advice would be useful. I have been searching for an answer and unable to find one. I have seen many questions and answers on StackOverflow about super and super in __init__ and super in Python, but nonw about whether there is syntax for calling super that does not require parameters to be specified and calls it with the same parameters as the given method was called in the child class.



What I am trying to do is very similar to raise in a try, except, raise construct in Python:



try:
linux_interaction()
except Exception as error:
logger.critical(str(error))
raise


In the above block I catch all exceptions, I log any exception as critical and then I throw it with just a call to raise, no parameters. That calling syntax of raise with no parameters causes the exception that was caught in the variable named error to be thrown and preserves the calling stack/stack trace. If one called raise error instead of just raise, then the stack trace from where the error originated would be lost.



I am trying to do something similar here, just invoke super and get the super method invoked with the same parameters as the given method without needing to know what they are and without needing to specify them -- that would be convenient, right?



Anyone know if this is possible and if so what the syntax in Python 2.7 would be?







python python-2.7 init super superclass






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 22:23









Peter JirakPeter Jirak

604




604







  • 2





    No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

    – Martijn Pieters
    Mar 22 at 22:30






  • 2





    Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

    – o11c
    Mar 22 at 22:30






  • 2





    The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

    – chepner
    Mar 22 at 22:30







  • 2





    I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

    – o11c
    Mar 22 at 22:55












  • 2





    No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

    – Martijn Pieters
    Mar 22 at 22:30






  • 2





    Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

    – o11c
    Mar 22 at 22:30






  • 2





    The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

    – chepner
    Mar 22 at 22:30







  • 2





    I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

    – o11c
    Mar 22 at 22:55







2




2





No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

– Martijn Pieters
Mar 22 at 22:30





No, you can't do this in Python. super() is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other.

– Martijn Pieters
Mar 22 at 22:30




2




2





Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

– o11c
Mar 22 at 22:30





Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless.

– o11c
Mar 22 at 22:30




2




2





The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

– chepner
Mar 22 at 22:30






The short answer is: no, you need to provide arguments to super in Python 2. Python's super is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support.

– chepner
Mar 22 at 22:30





2




2





I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

– o11c
Mar 22 at 22:55





I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. inspect.Signature doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 __class__ isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach.

– o11c
Mar 22 at 22:55












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308538%2fin-python2-can-i-invoke-super-in-init-other-methods-without-params-so-th%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308538%2fin-python2-can-i-invoke-super-in-init-other-methods-without-params-so-th%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript