大约有 13,340 项符合查询结果(耗时:0.0136秒) [XML]
List all base classes in a hierarchy of given class?
...gt;
>>> import inspect
>>> inspect.getmro(B)
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
share
|
improve this answer
|
...
Can a decorator of an instance method access the class?
...decorator, perhaps something like this (warning: untested code).
def class_decorator(cls):
for name, method in cls.__dict__.iteritems():
if hasattr(method, "use_class"):
# do something with the method and class
print name, cls
return cls
def method_decorator(v...
difference between variables inside and outside of __init__()
...
Variable set outside __init__ belong to the class. They're shared by all instances.
Variables created inside __init__ (and all other method functions) and prefaced with self. belong to the object instance.
...
What does “mro()” do?
...
Follow along...:
>>> class A(object): pass
...
>>> A.__mro__
(<class '__main__.A'>, <type 'object'>)
>>> class B(A): pass
...
>>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
>>> class C(A): pa...
How to print to console in pytest?
...as printed to standard out in that particular test.
For example,
def test_good():
for i in range(1000):
print(i)
def test_bad():
print('this should fail!')
assert False
Results in the following output:
>>> py.test tmp.py
============================= test session s...
How to import classes defined in __init__.py
...
'lib/'s parent directory must be in sys.path.
Your 'lib/__init__.py' might look like this:
from . import settings # or just 'import settings' on old Python versions
class Helper(object):
pass
Then the following example should work:
from lib.settings import Values
from l...
What is __init__.py for?
What is __init__.py for in a Python source directory?
12 Answers
12
...
Why does Python use 'magic methods'?
...s', e.g. to make its length available, an object implements a method, def __len__(self) , and then it is called when you write len(obj) .
...
NSLog the method name with Objective-C in iPhone
...
print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
Swift 3 and above
print(#function)
share
|
...
What does functools.wraps do?
...r. In other words, if you have a decorator
def logged(func):
def with_logging(*args, **kwargs):
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_logging
then when you say
@logged
def f(x):
"""does some math"""
return x + x * x
it's ex...