Python : 입력 매개 변수와 동일한 클래스의 여러 객체를 취하는 함수를 빌드하는 방법은 무엇입니까?

16180

파이썬에서 연결된 목록으로 작업하고 있습니다.

연결된 목록을 작성하기 위해 작성한 두 클래스는 다음과 같습니다.

class node:
    
    def __init__(self, value):
        self.value = value
        self.next = None

class linkedList:
    
    def __init__(self):
        self.head = None

# Two linked lists are being created:
l1 = linkedList() #1st linked list
l1.head = node(1)
new_node1 = node(2)
l1.head.next = new_node1

l2 = linkedList() #2nd linked list
l2.head = node(10)
new_node2 = node(20)
l2.head.next = new_node2

이제 두 개의 연결 목록을 가져 와서 비교, 연결 목록 연결 등과 같은 다양한 작업을 수행하는 linkedList 클래스 내부에 함수를 만들고 싶습니다.

그러나 주요 과제는 입력 매개 변수와 동일한 클래스의 여러 개체를 사용하는 함수를 작성하는 방법을 잘 모르겠다는 것입니다.

당신의 도움은 정말 감사하겠습니다.

미리 감사드립니다!

순종하다
class linkedList:
    
    #create function of linkedList class which takes exactly 2 linkedList objects:

    def function(obj1: linkedList, obj2: linkedList):
        pass #do whatever you need to do with those objects

이 기능을 적용 할 수 있습니다.

linkedList.function(LL1, LL2)

또는 다음과 같이 할 수 있습니다.

class linkedList:
    
    def function(self, obj: linkedList):
        pass

이렇게하면 호출 된 객체와 인수로 전달 된 다른 객체에 함수가 적용됩니다.

LL1.function(LL2)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관