python2 class 리스트,매트릭스 멤버간 합

class aaaa:
        def __init__(self,x):
                self.x =x
        def __myadd__(self,other):
                z = self.x+other
                return z
        def __addlst__(self,lst):
                for i in range(0,len(self.x)):
                        self.x[i] = self.x[i] + lst[i]
                return self.x
        def __addmatrix__(self,mat):
                for i in range(0,len(self.x)):
                        for j in range(0,len(self.x[i])):
                                self.x[i][j] = self.x[i][j] + mat[i][j]
                return self.x
        def __str__(self):
                return '[Well done: %s]' % self.x

--------------------------------------------------------------------------------------------
파일을 assignment.py로 저장
>>> from assignment import *
>>> a = aaaa([1,2,3])
>>> a
<assignment.aaaa instance at 0x0000000003A9CC88>
>>> a.x
[1, 2, 3]
>>> a.__myadd__([3,2,1])
[1, 2, 3, 3, 2, 1]
>>> a.__addlst__([3,2,1])
[4, 4, 4]
>>> a.x = [[1,2,3],[3,2,1]]
>>> a.x
[[1, 2, 3], [3, 2, 1]]
>>> a.__str__()
'[ThirdClass: [[1, 2, 3], [3, 2, 1]]]'
>>> a.__addmatrix__([[3,2,1],[1,2,3]])
[[4, 4, 4], [4, 4, 4]]

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

About G1 GC

sneak peek jitpack

About idempotent

C 언어 구조체의 포인터 멤버 변수

Synology NAS에 MariaDB 10에 Mysql workbench로 원격접속하기

About Websocket minimize data size and data transfer cost on cloud