내 문제는 다음과 같습니다. 나는 두 가지 방법으로 클래스 작업 창을 가지고 있습니다. 인스턴스화는해야하는대로 작동합니다. 이제 모든 인스턴스화 된 객체의 목록을 표시 할 때 객체 당 멤버 변수 (예 : _tp_nr)를 인쇄하고 싶습니다.파이썬 클래스 인스턴스 반환 값
다음 코드는 올바른 값을 반환하지만 이상한 (?) 형식의 반환 값을 얻습니다.
이
코드입니다 :#import weakref
class Taskpane():
'''Taskpane class to hold all catalog taskpanes '''
#'private' variables
_tp_nr = ''
_tp_title = ''
_tp_component_name = ''
#Static list for class instantiations
_instances = []
#Constructor
def __init__(self,
nr,
title,
component_name):
self._tp_nr = nr,
self._tp_title = title,
self._tp_component_name = component_name
#self.__class__._instances.append(weakref.proxy(self))
self._instances.append(self)
def __str__(self):
return str(self._tp_nr)
def setTaskpaneId(self, value):
self._tp_nr = value
def getTaskpaneId(self):
return str(self._tp_nr)
def setTaskpaneTitle(self, value):
self._tp_title = value
def getTaskpaneTitle(self):
return str(self._tp_title)
def setTaskpaneComponentName(self, value):
self._tp_component_name = value
def getTaskpaneComponentName(self):
return self._tp_component_name
tp1 = Taskpane('0', 'Title0', 'Component0')
tp2 = Taskpane('1', 'Title1', 'Component1')
#print Taskpane._instances
#print tp1
for instance in Taskpane._instances:
print(instance.getTaskpaneId())
for instance in Taskpane._instances:
print(instance.getTaskpaneTitle())
결과 :
('0',)
('1',)
('Title0',)
('Title1',)
질문은 : 는 왜 형식의이 종류의 결과를 반환합니까? 내가 볼에만 기대 : 사용하는 경우
'0'
'1'
('Title0')
('Title1')
:
for instance in Taskpane._instances:
print(instance._tp_nr)
결과는 동일합니다.
일부 구문 문제가 있으므로 답변을 다른 사람 (그러므로 내 downvote) 도움이되지 않습니다. – Alfe
@Alfe 이것이 문제가 될 수 있다는 것을 제외하면 ... – glglgl
그래,하지만 그건 모든 경우에 무작위로 발견 될거야. 제목이 "이 후행 쉼표로 작동하지 않는 이유"와 같은 sth 인 경우 모두 괜찮습니다. – Alfe