【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)
【334】Python Object-Oriented Programming的更多相关文章
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- 【转】Python 面向对象(初级篇)
[转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...
- 【转】Python之面向对象与类
[转]Python之面向对象与类 本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 继承层级关系中子类的实例对象对属性的查找顺序问题 一.面向对 ...
- 【转】Python函数默认参数陷阱
[转]Python函数默认参数陷阱 阅读目录 可变对象与不可变对象 函数默认参数陷阱 默认参数原理 避免 修饰器方法 扩展 参考 请看如下一段程序: def extend_list(v, li=[]) ...
- 【转】Python模块学习 - fnmatch & glob
[转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...
- 【转】python之模块array
[转]python之模块array >>> import array#定义了一种序列数据结构 >>> help(array) #创建数组,相当于初始化一个数组,如: ...
- 【转】python 面向对象(进阶篇)
[转]python 面向对象(进阶篇) 上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 ...
- 【转】Python基础-封装与扩展、静态方法和类方法
[转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...
- 【转】python类中super()和__init__()的区别
[转]python类中super()和__init__()的区别 单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(se ...
随机推荐
- PHP:第五章——字符串与数组及其他函数
<?php header("Content-Type:text/html;charset=utf-8"); //1.str_split——将字符串转换为数组. /*$str= ...
- 使用QQ邮箱SMTP服务的javamail配置
最近做一个小项目要用到JAVA的邮箱的发送功能.遇到一些坑这里记录分享一下:QQ群交流:697028234 1.QQ邮箱一定要设置开通SMTP/POP这项.并生成授权码. 2.用MAVEN生成一个QU ...
- Junit4与junt3并存时产生的问题
目前的项目里用junit写单元测试,使用的是junit4,由于大部分开发之前使用的都是junit3,对junit4还不是很熟悉,所以出现了junit3和4混合使用的情况,导致发生了一些问题,这里列举一 ...
- 关于CMD/DOS中的短文件名规则
最近在制作一个批处理的过程中发现一个很郁闷的问题,就是有些时候搜索到的结果不是我们想要的
- 【转】MongoDB 3.0 正式版本即将发布,强力推荐
MongoDB 今天宣布3.0 正式版本即将发布.这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统. MongoDB 3.0 在性能和伸缩性方面都有 ...
- 基于 React + NodeJS + Express + MongoDB 开发的一个社区系统
还可以, 功能挺全的, 可以作为react开发入门项目 链接 线上站点: 源码地址:
- php中require_once与include_once的区别
首先include_once仅包含文件一次,如果没有文件,会发出警告,并继续执行. 而require_once也是仅包含文件一次,但是如果程序中没有找到文件,则程序会中止执行.
- Oozie_02安装遇到错误【20161116】
[错误原因]hadoop的core-site.xml配置错误 把用户名hadoop配置成了主机名hadoop01 <!-- OOZIE --><property> <n ...
- B树、B-树、B+树、B*树都是什么
B树.B-树.B+树.B*树都是什么 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右 ...
- LOJ10131. 「一本通 4.4 例 2」暗的连锁【树上差分】
LINK solution 很简单的题 你就考虑实际上是对每一个边求出两端节点分别在两个子树里面的附加边的数量 然后这个值是0第二次随便切有m种方案,如果这个值是1第二次只有一种方案 如果这个值是2或 ...