我在教程中遇到了以下函数。当我调用函数时,没有打印"This prints a passed string into this function"。为什么函数调用时不打印这段文字?
def printme(str):
"This prints a passed string into this function"
print str
return
# Now you can call printme function
printme("I'm first call to user defined function!")
我正在处理Python3中的typing模块,看看我能在多大程度上扩展它的DSL。为了使这个DSL可用,我想提取一些用户定义的类的DocStrings,比如这个:
class MyRecord:
"""
This is just a foo.
Wouldn't it be cool if you could extract this DocString's text at runtime?
"""
foo: str
我一直在搜索并找到了,但它没有告诉我在这种情况下使用DocStrings的
当我在书中做一些练习--机器学习在行动中,在约会匹配问题中,我遇到了以下问题,但我不知道为什么!
Traceback (most recent call last):
File "(stdin)", line 1, in (module)
File 'kNN.py', line 27 ,in file2matrix
fr = open(filename)
TypeError: function takes at least 2 arguments (1 given)
下面是我的代码:
from numpy import *
im
比方说,我有这样的功能:
def myFunc():
# useful function to calculate stuff
这将产生缩进错误,除非我添加pass。
def myFunc():
# useful function to calculate stuff
pass
但是,如果我将注释替换为docstring,则不需要pass:
def myFunc():
"""useful function to calculate stuff"""
据我所知,这似乎是一个奇怪的特性,因为据我所知,这两种特性都没有在
我遇到了一个奇怪的错误,在注释出行中出现了“期望缩进块”- #______ICHIMOKU系统的COMPONENTS_____中心= False =查看是否。我不知道为什么。如果我删除这一行,并在该位置添加任何其他内容,它将开始在取代它的代码的下一部分中显示该错误。请帮帮忙。
"""
In this project I am going to import data from NSE, learn OOP and
try and learn how to implement various calculations
"""
import date
这个问题似乎经常出现在StackOverflow和其他地方,但我在任何地方都找不到一个完全令人满意的解决方案。
似乎有两种常见的解决方案。第一个(例如来自)使用了一个函数装饰器:
class SuperClass:
def my_method(self):
'''Has a docstring'''
pass
class MyClass(SuperClass):
@copy_docstring_from(SuperClass)
def my_method(self):
pa
我目前正在为Python开发一个基于C++的模块。我发现Boost::Python可以很好地完成我想要完成的任务。但是,我现在遇到了一些由Boost::Python生成的文档字符串的问题。给定以下Boost::Python定义:
BOOST_PYTHON_MODULE(gcsmt)
{
class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init)
.def("PrintSupported", &gcsmt::Units::printSup
在执行下面的代码时,我得到了AttributeError: attribute '__doc__' of 'type' objects is not writable。
from functools import wraps
def memoize(f):
""" Memoization decorator for functions taking one or more arguments.
Saves repeated api calls for a given value, by caching it.
我目前正在阅读使用字符串作为注释的python代码。例如,这里有一个函数
def xyz(x):
"""This is a function that does a thing.
Pretty cool, right?"""
return 0
使用字符串作为注释有什么意义?这看起来真的很奇怪。代码会编译,但非常令人困惑。
似乎有两个地方可以为一个类放置docstring:
在阶级定义下:
class MyClass(object):
""" Summary of MyClass
Body
...
"""
就在__init__构造函数下面:
...
def __init__(self, arg1, arg2):
""" Summary of MyClass
Body
...
""
我实现了一个抽象类: from abc import ABC, abstractmethod
class Study(ABC):
""" Define the purpose of the study here """
def __init__(self, value):
self.value = value
@abstractmethod
def do_something(self):
pass 我还实现了一个子类: class PilotStudy(Study):
def do
来自2.7.2文档,第6节,Modules
Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only __doc__ strings are removed from the bytecode, resulting in more compact .pyo files.
这引
我有一个模块errors.py,其中定义了几个全局常量(注意:我知道Python没有常量,但我已经按照惯例使用大写来定义它们)。
"""Indicates some unknown error."""
API_ERROR = 1
"""Indicates that the request was bad in some way."""
BAD_REQUEST = 2
"""Indicates that the request is missing required par
为什么字节字符串b'string'不能用作类或函数的文档字符串(从程度上说,我是在猜测模块)?
例如:
>>> class Foo:
... b"""My Foo class for Fooing"""
>>> Foo.__doc__ is None
True
对函数也是如此。
对于r''、u'' (预期)字符串,它可以正常工作。我在文件里也找不到任何东西。知道这是为什么吗?
假设我有这样一个函数:
def square(n):
'''Takes in a number n, returns the square of n'''
how_can_I_do_this = __doc__
有没有办法不提函数名就得到__doc__字符串?也就是说,如何在不指定函数名称的情况下打印函数内部的__doc__字符串?