我该如何定义一个函数,where它可以告诉它在哪里执行,没有传入参数?〜/ app /中的所有文件
a.py:
def where():
return 'the file name where the function was executed'
Run Code Online (Sandbox Code Playgroud)
b.py:
from a import where
if __name__ == '__main__':
print where() # I want where() to return '~/app/b.py' like __file__ in b.py
Run Code Online (Sandbox Code Playgroud)
c.py:
from a import where
if __name__ == '__main__':
print where() # I want where() to return '~/app/c.py' like __file__ in c.py
Run Code Online (Sandbox Code Playgroud) 我想将子类中的所有属性名称放入列表中,我想在基类中执行此操作。我怎样才能做到这一点?我现在的方法是:
class Base():
def __init__(self):
# print SubClass' new attribues' names ('aa' and 'bb' for example)
for attr in dir(self):
if not hasattr(Base, attr):
print attr
class SubClass(Base):
aa = ''
bb = ''
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?
谢谢你的帮助。