【334】Python Object-Oriented Programming
Reference: Python中self用法详解
- __init__ 方法;
- 私有变量。
Reference: 【290】Python 函数
- class 里面的 function 创建与此一致,只是会多一个 self 参数;
- 必备参数 —— 须以正确的顺序传入;
- 关键字参数 —— 允许函数调用时参数的顺序与声明时不一致;
- 缺省参数 —— 缺省参数的值如果没有传入,则被认为是默认值;
- 不定长参数 —— 加了星号(*)的变量名会存放所有未命名的变量参数;
- 匿名参数 —— 使用 lambda 来创建匿名函数;
- return 语句 —— return语句退出函数,选择性地返回一个表达式。
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | ''' 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) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)