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 idempotent

About G1 GC

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

sneak peek jitpack

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

DDD(Domain Driven Design) - Aggregate (어그리게잇)

Strategy Pattern In Spring (feat. JPA)