开发者

only() method in Python?

I have these lines in Python:

page = lxml.html.parse(URL).getroot()
table = only(page.cssselect('table[width=510]'))

What is the only method doing? I can't find it in the Python docs (though that might just 开发者_StackOverflowbe because it's very hard to search for!)

thanks.


There is no only built-in function, as you'll see if you type help(only) into your Python interpreter.

It must be pulled into the namespace with a from <module> import <only|*> instruction in that module. When you find this, you could try importing the module in your Python interpreter and using the help function again to find out what it does.


You can try to figure out which module only is defined in by examining the import statements in your file. Then look up only in the docs for that module. Or just put a print only.__module__ in your code and that might print out the module.


AFAIK, it isn't standard. But I'd guess that it fetches the only element from its input, or raises an exception if the input doesn't have exactly one element. E.g.:

>>> def only(input):
...     [result] = input
...     return result
... 
>>> only([12])
12
>>> only([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in only
ValueError: need more than 0 values to unpack
>>> only([23, 34])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in only
ValueError: too many values to unpack
>>> 


only is not a built-in function. Is it defined or imported anywhere in the .py file that you have? If not, look for from somemodule import *, and then look in each occurrence of somemodule.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜