docstring


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

How to print Docstring of python function from inside the function itself?

社会主义新天地 提交于 2019-12-28 11:43:11
问题 I want to print the docstring of a python function from inside the function itself. for eg. def my_function(self): """Doc string for my function.""" # print the Docstring here. At the moment I am doing this directly after my_function has been defined. print my_function.__doc__ But would rather let the function do this itself. I have tried calling print self.__doc__ print self.my_function.__doc__ and print this.__doc__ inside my_function but this did not work. 回答1: def my_func(): """Docstring

How to put a variable into Python docstring

 ̄綄美尐妖づ 提交于 2019-12-28 05:06:10
问题 So I'm trying to create a "dynamic" docstring which is something like this: ANIMAL_TYPES = ["mammals", "reptiles", "other"] def func(animalType): """ This is a sample function. @param animalType: "It takes one of these animal types %s" % ANIMAL_TYPES """ to basically let the docstring for @param animalType show whatever ANIMAL_TYPES has; so that when this variable is updated, the docstring will be updated automatically. Unfortunately, however, it doesn't seem working... Does anyone know if

工具导航Map