docstring

Doctest for nested docstring

喜欢而已 提交于 2020-01-23 06:17:22
问题 Suppose I have following code: def foo(s): """A dummy function foo. For example: >>> a = '''This is a test string line 1 This is a test string line 2 This is a test string line 3''' >>> foo(a) This is a test string line 1 This is a test string line 2 This is a test string line 3 >>> """ print s if __name__ == '__main__': import doctest doctest.testmod() And let's save it as foo.py. When I run: C:\Python27>python.exe foo.py **********************************************************************

Doctest for nested docstring

删除回忆录丶 提交于 2020-01-23 06:16:31
问题 Suppose I have following code: def foo(s): """A dummy function foo. For example: >>> a = '''This is a test string line 1 This is a test string line 2 This is a test string line 3''' >>> foo(a) This is a test string line 1 This is a test string line 2 This is a test string line 3 >>> """ print s if __name__ == '__main__': import doctest doctest.testmod() And let's save it as foo.py. When I run: C:\Python27>python.exe foo.py **********************************************************************

Python set docstring and get method name of dynamically generated classmethod

一笑奈何 提交于 2020-01-14 03:28:26
问题 I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[0][3]) print "kwargs:", kwargs function_list = ['myF1', 'myF2'] for func in function_list: setattr(test, func, genericFunc) #set docstring for func here? if __name__ == '__main__': x = test() print "docstring:", x

What's the advantage of having multi-line & single-line string literals in python?

隐身守侯 提交于 2020-01-11 10:24:27
问题 I know the triple quote strings are used as docstrings, but is there a real need to have two string literals? Are there any use case when identifying between single-line & multi-line is useful. in Clojure we have 1 string literal, is multi-line and we use it as docstring. So why the difference in python? 回答1: The advantage of having to be explicit about creating a multi-line string literal is probably best demonstrated with an example: with open("filename.ext) as f: for line in f: print(line

Implementing a class property that preserves the docstring

為{幸葍}努か 提交于 2020-01-11 09:42:10
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Implementing a class property that preserves the docstring

穿精又带淫゛_ 提交于 2020-01-11 09:42:08
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Implementing a class property that preserves the docstring

懵懂的女人 提交于 2020-01-11 09:42:08
问题 I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already

Are Python docstrings and comments stored in memory when a module is loaded?

℡╲_俬逩灬. 提交于 2019-12-30 08:12:26
问题 Are Python docstrings and comments stored in memory when a module is loaded? I've wondered if this is true, because I usually document my code well; may this affect memory usage? Usually every Python object has a __doc__ method. Are those docstrings read from the file, or processed otherwise? I've done searches here in the forums, Google and Mailing-Lists, but I haven't found any relevant information. Do you know better? 回答1: By default, docstrings are present in the .pyc bytecode file, and

Are Python docstrings and comments stored in memory when a module is loaded?

半世苍凉 提交于 2019-12-30 08:11:27
问题 Are Python docstrings and comments stored in memory when a module is loaded? I've wondered if this is true, because I usually document my code well; may this affect memory usage? Usually every Python object has a __doc__ method. Are those docstrings read from the file, or processed otherwise? I've done searches here in the forums, Google and Mailing-Lists, but I haven't found any relevant information. Do you know better? 回答1: By default, docstrings are present in the .pyc bytecode file, and

Documenting `tuple` return type in a function docstring for PyCharm type hinting

穿精又带淫゛_ 提交于 2019-12-30 03:44:33
问题 How can I document that a function returns a tuple in such a way that PyCharm will be able to use it for type hinting? Contrived example: def fetch_abbrev_customer_info(customer_id): """Pulls abbreviated customer data from the database for the Customer with the specified PK value. :type customer_id:int The ID of the Customer record to fetch. :rtype:??? """ ... magic happens here ... return customer_obj.fullname, customer_obj.status #, etc. 回答1: I contacted PyCharm support, and this is what