Python как достать название полей класса
class W:
def __init__(self, first, second):
self.first = first
self.second = second
как сделать output "first, second"
Ответы (2 шт):
Автор решения: asanisimov
→ Ссылка
class W:
def __init__(self, first, second):
self.first = first
self.second = second
w = W(1, 2)
result = ', '.join([attr for attr in dir(w) if not attr.startswith('__')])
result_value = ', '.join([str(getattr(w, attr)) for attr in dir(w) if not attr.startswith('__')])
print(result) # first, second
print(result_value) # 1, 2
Первый результат - все атрибуты класса. Второй - все их значения.
Автор решения: entithat
→ Ссылка
class W:
def __init__(self, first, second):
self.first = first
self.second = second
W(None, None).__dict__.keys()