Drkcore

30 03 2009 lisp Python Tweet

Common Lispとpythonのdocstring

Common Lispのdocstringについて調べてたのだけど、pythonって関数にしかdocstringを入れられないんだっけ?と思ったのでやってみた。

まず関数。__doc__でdocstring。

>>> def sumab (a,b):
...   "test sum"
...   return a + b
... 
>>> print sumab.__doc__
test sum

まぁ、ここまではok

続いて変数

>>> a = 10
>>> dir(a)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', 
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', 
...
'conjugate', 'denominator', 'imag', 'numerator', 'real']

おー__doc__あるじゃん。

>>> print (a.__doc__)
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
non-string.  If base is zero, the proper base is guessed based on the
string content.  If the argument is outside the integer range a
long object will be returned instead.

ふむ、これは任意のstringに置き換えられるのかな?

>>> a.__doc__ = "test int"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object attribute '__doc__' is read-only

リードオンリーだった。str,float,listも一緒だった。

プリミティブな型にもdocstringはあるけど、それは書き換えられないってことかな。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021