0

Lawrence Benson points out that if you don't include an __init__ in your class, then it will inherit from a superclass by default in this post.

Python Docs supports this when stating that, "a class may define a special method named __init__()."

Is there any way to print/display a description of the __init__() or __new__() methods that apply to a particular class? Meaning - in the interpreter, can I run a command that will return them?

I've tried to run everything I can find in __dir__, to no avail:

>>>__dir__ 
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'drive']
jwodder
  • 54,758
  • 12
  • 108
  • 124
AFK
  • 412
  • 6
  • 12
  • 1
    I'm not sure this answers your question, but if you use `help` you can get information on the methods. For instance if you want info on `int`'s `__init__` method you could do `help(int.__init__)` – Cory Madden Jul 23 '17 at 16:50
  • What do you mean by description? The docstring? If so, why `__doc__` doesn't suit your needs? – EsotericVoid Jul 23 '17 at 16:51
  • Additionally using `__doc__` will get just the string. `>>> int.__init__.__doc__` `'Initialize self. See help(type(self)) for accurate signature.'` – Cory Madden Jul 23 '17 at 16:52
  • @Bit - I'm trying to get a printout of the init method that was used to initialize the subclass. It could be a method that was written specifically for the subclass, or it could be the init method that was inherited from a superclass. Either way, I want to be able to see the contents of the initialization method that was used. – AFK Jul 23 '17 at 16:56
  • @AFK: so you want to see the source code? – Martijn Pieters Jul 23 '17 at 16:59
  • @MartijnPieters If this is a duplicate, where's the original version? @AFK, you can access an instance's `__init__` doc string with `instance.__init__.__doc__` – Cory Madden Jul 23 '17 at 17:03
  • @MartijnPieters That would probably fry my brain, but maybe so. – AFK Jul 23 '17 at 17:03
  • @AFK: *I want to be able to see the contents of the initialization method that was used.*; that's the source code. – Martijn Pieters Jul 23 '17 at 17:05
  • @CoryMadden Your suggestion gets me really close to what I was looking for Cory. When I use print(subclass.__init__), I get this back: --- this tells me which init module is being applied to the subclass, and that's the information that I seek. Thank you! – AFK Jul 23 '17 at 17:05
  • Great! you're welcome. – Cory Madden Jul 23 '17 at 17:08
  • @MartijnPieters - thanks for that too. I'll dig in on the source code a bit to get a better understanding. – AFK Jul 23 '17 at 17:12

0 Answers0