create account

Python Rules Of Coding: Docstrings by alrashel

View this thread on: hive.blogpeakd.comecency.com
· @alrashel · (edited)
$3.08
Python Rules Of Coding: Docstrings
previous 
[Python Rules Of Coding: String literals](https://hive.blog/hive-122108/@alrashel/python-rules-of-coding-string-literals)

![code-1084923_1280.png](https://images.hive.blog/DQmUZ6UfTSYvnT52mhXuoTfb5LG8TXNHfJ8BVktt7zkXhAh/code-1084923_1280.png)

<center><b>[Source](https://pixabay.com/photos/code-programming-python-1084923/)</b></center>

#### <center>Python Rules Of Coding: Docstrings</center>
Python docstring (documentation string) is a string literal and it can be used in any code block, i.e., the class, module, function, method definition. It provides us a handy way of attaching the relevant documentation within the code block. It describes the source code.

We generally use comments for documentation of a single line code, statement, and expressions which are usually small and not so explanatory. 
>Remember, this does not diminish the importance of comments use.

 Docstrings are a descriptive text and better for easily understanding the functionality, the purpose of the code block. 

When a programmer creates class, module, function, he/she associates the documentation for another developer to know what the purpose or what does of that class, module, function. 

So that other developer who wishes to contribute to that project can easily grasp the understanding of this code block without having to read the details of the implementation. 

We can use Python documentation generators like [Sphinx](https://www.sphinx-doc.org/en/master/) to generate HTML documentation automatically from Docstrings. 

The source code documentation (Docstrings) can be accessed using the built-in `__doc__ `attribute or using the `help` function. 

>As [PEP 257 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/), we should use 
 " " " triple double quotes " " " in docstrings. 
  r " " " raw triple double quotes " " "  escape backslashes in docstrings.
 u" " " Unicode triple quoted strings" " "  for unicode docstrings. 

We are familiar with two forms of docstrings.
1. One-line Docstrings
2. Multi-line Docstrings

#### One-line Docstrings :  

    
    class Sensor:
         def __init__(self, calc=0.0):
        “ “ “Initialize the Sensor object with the given warmth value.” “ “
        
         self.set_temperature(Calc)
         return


Notes from [PEP 257 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/) :
1. Use triple double-quotes.
2. Keep the closing quotes in the same line as with the opening quotes.
3. No need a blank line either before or after the docstring.

```
def square(b):
       “ “ “ Returned argument b is squared.” “ “
      return b**b
      print(square.__doc__)    #using __doc__ attribute
````
#### Output
    Returned argument b is squared.

We can also access to the docstring using the built-in `help` function
````
def square(b):
     “ “ “ Returned argument b is squared.” “ “
    return b**b
   help(square)         # using help function
````
#### Output
````
Help on function square in module __main__:

square(b)
    Returned argument a is squared.
````

#### Multi-line Docstring:

The form of multi-line docstrings is the same as a one-line docstring, but it is a more elaborate description. 
````
def  my_func(dev1): 
   """ 
   Summary line. 

   Elaborated description of function. 

   Parameters: 
   dev1(int): Description of dev1 

   Returns: 
   int: Description of return value 

   """

   return  dev1

   print my_func.__doc
````
#### Output
````
Summary line. 

   Elaborated description of function. 

   Parameters: 
   dev1(int): Description of dev1 

   Returns: 
   int: Description of return value
````


<center>______________________</center>

#### Next
##### Operator Precedence
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 233 others
properties (23)
authoralrashel
permlinkpython-rules-of-coding-docstrings
categoryhive-122108
json_metadata{"app":"hiveblog/0.1","format":"markdown","image":["https://images.hive.blog/DQmUZ6UfTSYvnT52mhXuoTfb5LG8TXNHfJ8BVktt7zkXhAh/code-1084923_1280.png"],"links":["https://hive.blog/hive-122108/@alrashel/python-rules-of-coding-string-literals","https://pixabay.com/photos/code-programming-python-1084923/","https://www.sphinx-doc.org/en/master/","https://www.python.org/dev/peps/pep-0257/"],"tags":["technology","tutorial","stem","dev","programming","python"]}
created2020-05-07 10:49:09
last_update2020-05-08 05:34:45
depth0
children9
last_payout2020-05-14 10:49:09
cashout_time1969-12-31 23:59:59
total_payout_value1.564 HBD
curator_payout_value1.516 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,704
author_reputation2,707,464,674,673
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,237,029
net_rshares6,363,897,505,270
author_curate_reward""
vote details (297)
@adexbafo ·
This is comprehensive. Thanks
properties (22)
authoradexbafo
permlinkre-alrashel-202058t34536447z
categoryhive-122108
json_metadata{"tags":["stem","technology","palnet","neoxian","dev","programming","python"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-05-08 02:45:39
last_update2020-05-08 02:45:39
depth1
children1
last_payout2020-05-15 02:45:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation270,810,806,579
root_title"Python Rules Of Coding: Docstrings"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,250,517
net_rshares0
@alrashel ·
Welcome friend
properties (22)
authoralrashel
permlinkq9zzmq
categoryhive-122108
json_metadata{"app":"hiveblog/0.1"}
created2020-05-08 05:37:39
last_update2020-05-08 05:37:39
depth2
children0
last_payout2020-05-15 05:37:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14
author_reputation2,707,464,674,673
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,252,344
net_rshares0
@ahai5907 ·
Always upvote
👎  
properties (23)
authorahai5907
permlinkre-alrashel-202057t1979101z
categoryhive-122108
json_metadata{"tags":["stem","technology","palnet","neoxian","dev","programming","python"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-05-07 13:07:12
last_update2020-05-07 13:07:12
depth1
children1
last_payout2020-05-14 13:07:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation166,725,839,505
root_title"Python Rules Of Coding: Docstrings"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,238,891
net_rshares0
author_curate_reward""
vote details (1)
@alrashel ·
Thanks
properties (22)
authoralrashel
permlinkq9zzj5
categoryhive-122108
json_metadata{"app":"hiveblog/0.1"}
created2020-05-08 05:35:30
last_update2020-05-08 05:35:30
depth2
children0
last_payout2020-05-15 05:35:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation2,707,464,674,673
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,252,325
net_rshares0
@anicom ·
Wow.  Amazing one. 
👍  
properties (23)
authoranicom
permlinkre-alrashel-q9z9t2
categoryhive-122108
json_metadata{"tags":["hive-122108"],"app":"peakd/2020.04.5"}
created2020-05-07 20:19:57
last_update2020-05-07 20:19:57
depth1
children1
last_payout2020-05-14 20:19:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length19
author_reputation15,007,173,666,572
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,246,121
net_rshares1,132,039,010
author_curate_reward""
vote details (1)
@alrashel ·
Thanks
properties (22)
authoralrashel
permlinkq9zzlz
categoryhive-122108
json_metadata{"app":"hiveblog/0.1"}
created2020-05-08 05:37:12
last_update2020-05-08 05:37:12
depth2
children0
last_payout2020-05-15 05:37:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation2,707,464,674,673
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,252,340
net_rshares0
@goodguymate ·
Once you understand how to do it it's pretty easy, isn't it?
Python as whole feels like that to me
properties (22)
authorgoodguymate
permlinkre-alrashel-202057t1815530z
categoryhive-122108
json_metadata{"tags":["stem","technology","palnet","neoxian","dev","programming","python"],"app":"esteem/2.2.5-mobile","format":"markdown+html","community":"hive-125125"}
created2020-05-07 16:01:00
last_update2020-05-07 16:01:00
depth1
children1
last_payout2020-05-14 16:01:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length98
author_reputation1,094,418,254,106
root_title"Python Rules Of Coding: Docstrings"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,241,656
net_rshares0
@alrashel ·
Absolutely right, friend.
properties (22)
authoralrashel
permlinkq9zzl8
categoryhive-122108
json_metadata{"app":"hiveblog/0.1"}
created2020-05-08 05:36:45
last_update2020-05-08 05:36:45
depth2
children0
last_payout2020-05-15 05:36:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length25
author_reputation2,707,464,674,673
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,252,335
net_rshares0
@hivebuzz ·
Congratulations @alrashel! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@alrashel/upvoted.png?202005080216"></td><td>You received more than 1500 upvotes. Your next target is to reach 1750 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your board](https://hivebuzz.me/@alrashel) and compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @hivebuzz:**
<table><tr><td><a href="/revolution/@hivebuzz/revolution-revolution"><img src="https://images.hive.blog/64x128/https://i.imgur.com/kvCt22V.png"></a></td><td><a href="/revolution/@hivebuzz/revolution-revolution">Revolution! Revolution!</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-alrashel-20200508t022147000z
categoryhive-122108
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-05-08 02:21:45
last_update2020-05-08 02:21:45
depth1
children0
last_payout2020-05-15 02:21:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length921
author_reputation370,784,617,093,775
root_title"Python Rules Of Coding: Docstrings"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id97,250,300
net_rshares0