【334】Python Object-Oriented Programming

Reference: Python中self用法详解

  • __init__ 方法;
  • 私有变量。

Reference: 【290】Python 函数

  • class 里面的 function 创建与此一致,只是会多一个 self 参数;
  • 必备参数 —— 须以正确的顺序传入;
  • 关键字参数 —— 允许函数调用时参数的顺序与声明时不一致;
  • 缺省参数 —— 缺省参数的值如果没有传入,则被认为是默认值;
  • 不定长参数 —— 加了星号(*)的变量名会存放所有未命名的变量参数;
  • 匿名参数 —— 使用 lambda 来创建匿名函数;
  • return 语句 —— return语句退出函数,选择性地返回一个表达式。

Example:

'''
Created on 2018年9月18日

@author: McDelfino
'''

class Node:
    def __init__(self, value):
        self.value = value
        self.next_node = None
        
n1 = Node(10)
print(n1.value)
n2 = Node(15)

n1.next_node = n2
print(n1.next_node.value)

n3 = Node(11)
n2.next_node = n3

print(n1.next_node.next_node.value)


class LinkedList:
    def __init__(self, L = None, *, key = lambda x: x):
        if not L:
            self.head = None
            return
        self.key = key
        self.head = Node(L[0])
        current_node = self.head
        for e in L[1: ]:
            current_node.next_node = Node(e)
            current_node = current_node.next_node
     
    def display(self, separator = ', '):
        E = []
        current_node = self.head
        while current_node:
            E.append(current_node.value)
            current_node = current_node.next_node
        print(separator.join(str(e) for e in E))
        
    def __len__(self):
        if not self.head:
            return 0
        length = 0
        current_node = self.head
        while current_node.next_node:
            length += 1
            current_node = current_node.next_node
        return length 
    
    def append(self, value):
        new_node = Node(value)
        if not self.head:
            self.head = new_node
            return
        current_node = self.head
        while current_node.next_node:
            current_node = current_node.next_node
        current_node.next_node = new_node
    
    def insert_at_beginning(self, value):
        new_node = Node(value)
        if not self.head:
            self.head = new_node
            return
        new_node.next_node = self.head
        self.head = new_node
    
    def insert_value_before(self, value_1, value_2):
        if not self.head:
            return False
        if self.head.value == value_2:
            new_node = Node(value_1)
            new_node.next_node = self.head
            self.head = new_node
            return True
        current_node = self.head
        while current_node.next_node and
             current_node.next_node.value != value_2:
            current_node = current_node.next_node
        if current_node.next_node and
            current_node.next_node.value == value_2:
            new_node = Node(value_1)
            new_node.next_node = current_node.next_node
            current_node.next_node = new_node
            return True
        return False    
    
    def is_sorted(self):
        if len(self) < 2:
            return True
        current_node = self.head
        while current_node.next_node:
            if self.key(current_node.value) >
                self.key(current_node.next_node.value):
                return False
            current_node = current_node.next_node
        return True
    
    def reverse(self):
        self.display()
        if len(self) < 2:
            return
        current_node = self.head
        while current_node.next_node.next_node:
            current_node = current_node.next_node
        last_node = current_node.next_node
        current_node.next_node = None
        self.reverse()
        last_node.next_node = self.head
        self.head = last_node
        
LL = LinkedList([1, 10, 4, 6])
LL.display()
print('--------------------')
print(LL.is_sorted())
LL.reverse()
print('--------------------')
LL.display()
LL.display('---')
LL.display()
print(len(LL))
LL.append(7)
LL.display()
LL.insert_at_beginning(23)
LL.display()
LL.insert_value_before(-10, 1)
LL.display()
LL.insert_value_before(63, 10)
LL.display()
print(LL.head.value)
print(LL.head.next_node.value)
print(LL.head.next_node.next_node.value)
原文地址:https://www.cnblogs.com/alex-bn-lee/p/9672472.html