小编wal*_*tes的帖子

确定函数的执行位置?

我该如何定义一个函数,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)

python

6
推荐指数
1
解决办法
401
查看次数

Python:如何在基类的方法中获取子类的新属性名称?

我想将子类中的所有属性名称放入列表中,我想在基类中执行此操作。我怎样才能做到这一点?我现在的方法是:

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)

有更好的方法吗?


谢谢你的帮助。

python attributes subclass

5
推荐指数
1
解决办法
7465
查看次数

标签 统计

python ×2

attributes ×1

subclass ×1