-2

I have the following valid Python code:

class A:
    def __init__(self):
        """
        Some doc-string
        """
    def some_method(self):
        """
        Some other doc-string
        """

def say():
    """
    Well that's it, no doc-string
    """

a = A()
print('works')

And it outputs works.

But I'm not able to understand how is this a perfectly valid Python code. Can somebody please help me to understand its functioning?

Also, are there any PEPs which discourage/disallow such code?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Yash Ranjan
  • 75
  • 1
  • 3
  • 1
    Why would you expect this to *not* work? What expectation of yours is being violated here? – deceze Jun 20 '23 at 09:16
  • 1
    Yes, my understanding was that docstrings were different construct altogether, and I completely missed to understand that it's nothing but just a special string literal. Thanks for the help. – Yash Ranjan Jun 20 '23 at 12:06
  • 2
    Worth saying that in many languages, the equivalent of a docstring actually *is* just a specially formatted comment (e.g. a Java comment starts with `/*`, and if you put another `*` right after that, then Javadoc picks up the comment for inclusion in the documentation). So it makes sense that the behaviour described in the question might surprise someone coming to Python. – slothrop Jun 20 '23 at 14:56

2 Answers2

1

Empty functions and methods with only comments would fail, and people generally think of docstrings as being multi-line comments. What's actually happening here, though is that functions and classes in python are objects like anything else, and under the hood, they have an attribute .doc. When you add the docstring, you're telling python to do something, specifically to set .doc equal to the given string literal. So, for example:

class  MyClass:
  def mymethod(self):
    self.__str__ = "Foo"

mymethod here is valid, because the method does something. It sets self.str to "Foo". Similarly:

class  MyClass:
  def mymethod(self):
    self.__doc__ = "Foo"

would also be valid. Under the hood, that's the same as:

class  MyClass:
  def mymethod(self):
    '''
    Foo
    '''

So that's valid as well.

class  MyClass:
  def __init__():
    '''
    Foo
    '''
  def mymethod(self):
    '''
    Bar
    '''
cls = MyClass()
help(cls)
cls.mymethod()
help(cls)

...demonstrates this behavior.

Kyle Alm
  • 587
  • 3
  • 14
-2

Well this outputs works because you declare an empty class and then print 'works' What you would like to print eventually is your variable :

print(a)

a that should output:

If you want to go further you can add in your init function a variable that is manually set such as:

def __init__(self):
     self.test = 'foo'

Then print(a.test) and you will have 'foo'

Here more about python classes and object oriented programming

Max Alonzo
  • 45
  • 5
  • 3
    OP's question isn't really "how do I print a variable?" OP is trying to understand why it's valid for a function body to contain only a docstring, whereas it is not valid for it to contain only a comment, or to contain nothing at all. – slothrop Jun 20 '23 at 09:37