많은 사람들이 모든 클래스뿐만 참조용이므로 추출, ve seen i& # 39 에서 모듈에서는 일반적으로 다음과 같은 항목을
# foo.py
class Foo:
pass
# test.py
import inspect
import foo
for name, obj in inspect.getmembers(foo):
if inspect.isclass(obj):
print obj
멋집니다.
하지만 난 can& # 39 에서 볼 수 있는 방법을 찾을 수 없다, 모든 클래스뿐만 current 모듈에서는.
# foo.py
import inspect
class Foo:
pass
def print_classes():
for name, obj in inspect.getmembers(???): # what do I do here?
if inspect.isclass(obj):
print obj
# test.py
import foo
foo.print_classes()
이것은 아마도 haven& # 39, t, but I 명백하네 게 정말 아무것도 찾을 수 있었습니다. 아무나 돕는다구요 아웃해야?
이거 드세요.
import sys
current_module = sys.modules[__name__]
In your 컨텍스트로:
import sys, inspect
def print_classes():
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
print(obj)
심지어 우수:
clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
제임스이제멤버스 때문에 ' ()' 는 조건자.
['방향'] 에서 볼 수 있었는데 제가 필요한 모든 (https://docs.python.org/2/library/functions.html # dir) 내장 구성형 ['그레타르'] (https://docs.python.org/2/library/functions.html # 그레타르).
# Works on pretty much everything, but be mindful that
# you get lists of strings back
print dir(myproject)
print dir(myproject.mymodule)
print dir(myproject.mymodule.myfile)
print dir(myproject.mymodule.myfile.myclass)
# But, the string names can be resolved with getattr, (as seen below)
하지만, 털 않니다 보았으매 나올 것 같다.
def list_supported_platforms():
"""
List supported platforms (to match sys.platform)
@Retirms:
list str: platform names
"""
return list(itertools.chain(
*list(
# Get the class's constant
getattr(
# Get the module's first class, which we wrote
getattr(
# Get the module
getattr(platforms, item),
dir(
getattr(platforms, item)
)[0]
),
'SYS_PLATFORMS'
)
# For each include in platforms/__init__.py
for item in dir(platforms)
# Ignore magic, ourselves (index.py) and a base class.
if not item.startswith('__') and item not in ['index', 'base']
)
))
모든 클래스에 속한 할 경우, 현재 모듈에서는 이 사용할 수 있습니다.
import sys, inspect
def print_classes():
is_class_member = lambda member: inspect.isclass(member) and member.__module__ == __name__
clsmembers = inspect.getmembers(sys.modules[__name__], is_class_member)
답변 및 Nadia& 사용하는 경우, s # 39 에 있는 다른 클래스 임포트하지 조교하실 모듈에서는 적립율은 클래스뿐만 가져오는 것.
그래서 '왜' 이 추가되고, s = name member.module that& # 39 에 사용되는 조건자 is_class_member ''. 이 같은 발언은 정말 클래스에 속한 모듈에서는 있는지 확인합니다.
부울 값을 반환하는 함수 (호출) 는 한 조건자
파이썬 2 에서 작동하는 다른 솔루션 및 3:은
#foo.py
import sys
class Foo(object):
pass
def print_classes():
current_module = sys.modules[__name__]
for key in dir(current_module):
if isinstance( getattr(current_module, key), type ):
print(key)
# test.py
import foo
foo.print_classes()
이것은 내가 줄이 얻기 위해 사용하는 모든 클래스가 정의하십시오 현재 모듈 (ie 가져오지 않습니다. # 39 에 따르면 it& 조금 긴 하지만 적절하게 pep 8 변경할 수 있습니다.
import sys
import inspect
classes = [name for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isclass)
if obj.__module__ is __name__]
이 목록은 클래스 이름을 알 수 있습니다. 스케쳐내 클래스 객체 자체는 그냥 계속 obj 한다.
classes = [obj for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isclass)
if obj.__module__ is __name__]
이는 그동안 내 더욱 유용하게 사용할 수 있습니다.