大约有 43,000 项符合查询结果(耗时:0.0396秒) [XML]
What is a “callable”?
...allable is anything that can be called.
The built-in callable (PyCallable_Check in objects.c) checks if the argument is either:
an instance of a class with a __call__ method or
is of a type that has a non null tp_call (c struct) member which indicates callability otherwise (such as in functions,...
Understanding Python super() with __init__() methods [duplicate]
...ady.
Note that the syntax changed in Python 3.0: you can just say super().__init__() instead of super(ChildB, self).__init__() which IMO is quite a bit nicer. The standard docs also refer to a guide to using super() which is quite explanatory.
...
How to implement __iter__(self) for a container object (Python)
...owing will create an iterator that yields five, and then every item in some_list.
def __iter__(self):
yield 5
yield from some_list
Pre-3.3, yield from didn't exist, so you would have to do:
def __iter__(self):
yield 5
for x in some_list:
yield x
...
What is __future__ in Python used for and how/when to use it, and how it works
__future__ frequently appears in Python modules. I do not understand what __future__ is for and how/when to use it even after reading the Python's __future__ doc .
...
Build a Basic Python Iterator
... to the iterator protocol, which basically means they provide two methods: __iter__() and __next__().
The __iter__ returns the iterator object and is implicitly called
at the start of loops.
The __next__() method returns the next value and is implicitly called at each loop increment. This met...
Calling class staticmethod within the class body?
...
staticmethod objects apparently have a __func__ attribute storing the original raw function (makes sense that they had to). So this will work:
class Klass(object):
@staticmethod # use as decorator
def stat_func():
return 42
_ANS = stat_func...
__lt__ instead of __cmp__
Python 2.x has two ways to overload comparison operators, __cmp__ or the "rich comparison operators" such as __lt__ . The rich comparison overloads are said to be preferred, but why is this so?
...
Python (and Python C API): __new__ versus __init__
The question I'm about to ask seems to be a duplicate of Python's use of __new__ and __init__? , but regardless, it's still unclear to me exactly what the practical difference between __new__ and __init__ is.
...
what does the __file__ variable mean/do?
...
When a module is loaded from a file in Python, __file__ is set to its path. You can then use that with other functions to find the directory that the file is located in.
Taking your examples one at a time:
A = os.path.join(os.path.dirname(__file__), '..')
# A is the par...
What are “first class” objects?
...ally has a fairly rich and sophisticated interface.
>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '...