저는 Python과 OOP의 새로운 기능을 일반적으로 사용합니다. "...instance has no attribute '__getitem__'"
에 오류가 있으며 작성한 개체가 목록이 아니라는 것을 알고 있습니다. 어떻게하면 목록 객체가 될 수 있습니다. 여기서, 클래스 파일이다목록 개체를 만드는 방법은 무엇입니까? 인스턴스에 '__getitem__'속성이 없습니다.
#!/usr/bin/python -tt
import math, sys, matrix, os
class Point:
'Class for points'
pointCount = 0
def __init__(self, x, y, z):
'initialise the Point from three coordinates'
self.x = x
self.y = y
self.z = z
Point.pointCount += 1
def __str__(self):
'print the Point'
return 'Point (%f, %f, %f)' %(self.x, self.y, self.z)
def copyPoint(self, distance):
'create another Point at distance from the self Point'
return Point(self.x + distance[0], self.y + distance[1], self.z + distance[2])
def __del__(self):
'delete the Point'
Point.pointCount -= 1
#print Point.pointCount
return '%s deleted' %self
I는 (X, Y, Z) 안에 3 개 개의 좌표 점으로 있어야하고, 이들 좌표는 []로 목록 인스턴스 같은 "호출"이어야한다.
비슷한 주제를 읽었지만 많이 이해하지 못했습니다. 간단한 단어와 예를 들어 설명하십시오.
가def __getitem__(self, item):
return (self.x, self.y, self.z)[item]
이것은 X, Y 및 Z의 tuple
구축 한 액세스 파이썬 자체의 인덱싱 기능을 사용
거리로 무엇을 전달합니까? 뭔가 색인을 달 수 있습니까? 실제로'Point'를리스트 (예 : point [0]'등)로 사용하려고합니까? –
답변은 제목에 있습니다. – Marcin
__getitem__ : http : // stackoverflow를 구현해야합니다.com/questions/2936863/python-implementation-slicing-in-getitem –