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的更多相关文章

  1. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  2. 【转】Python 面向对象(初级篇)

    [转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...

  3. 【转】Python之面向对象与类

    [转]Python之面向对象与类 本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 继承层级关系中子类的实例对象对属性的查找顺序问题 一.面向对 ...

  4. 【转】Python函数默认参数陷阱

    [转]Python函数默认参数陷阱 阅读目录 可变对象与不可变对象 函数默认参数陷阱 默认参数原理 避免 修饰器方法 扩展 参考 请看如下一段程序: def extend_list(v, li=[]) ...

  5. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  6. 【转】python之模块array

    [转]python之模块array >>> import array#定义了一种序列数据结构 >>> help(array) #创建数组,相当于初始化一个数组,如: ...

  7. 【转】python 面向对象(进阶篇)

    [转]python 面向对象(进阶篇) 上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 ...

  8. 【转】Python基础-封装与扩展、静态方法和类方法

    [转]Python基础-封装与扩展.静态方法和类方法 一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数 ...

  9. 【转】python类中super()和__init__()的区别

    [转]python类中super()和__init__()的区别 单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(se ...

随机推荐

  1. easyui panel自适应浏览器宽度

    一.目标效果: 当浏览器窗口大小改变时.panel宽度始终为浏览器宽度的50%,panel高度则根据其中内容的多少而变化,横向竖向滚动条皆不出现.且不需要重新刷新浏览器或者其他js代码 兼容:chro ...

  2. ANSI的Escape序列屏幕控制码

    http://blog.csdn.net/lano2088/article/details/51985563 https://www.cnblogs.com/pied/p/4175641.html h ...

  3. php中require_once与include_once的区别

    首先include_once仅包含文件一次,如果没有文件,会发出警告,并继续执行. 而require_once也是仅包含文件一次,但是如果程序中没有找到文件,则程序会中止执行.

  4. Linux的发行版之间的联系和区别

    转载:https://blog.csdn.net/suixin788/article/details/52555558 联系 Linux的内核源代码和Linux的应用程序都可以自由获得,因此很多公司组 ...

  5. 前后端分离之让前端开发脱离接口束缚(mock)

    情景:     领导:小吴啊,最近在忙什么啊?     前吴:(心想:我擦勒,难道划水被领导发现了?也不能怪我啊,后台的哥们接口还没给呢,但要是实话实说不就对不起后台哥们了吗?)           ...

  6. [TopCoder11557]MatrixPower

    vjudge description 你有一个\(n \times n\)的矩阵\(A\),下标从\(0\)开始,其中\(A_{i,j}=di + q^j\). 给你\(d,q,n,k,s,t\),求 ...

  7. Linux内核调试

    1.控制台优先级配置cat /proc/sys/kernel/printk6 4 1 76是控制台的优先级,打印信息的优先级要比它高才能打印出.4是默认的优先级cat /var/log/message ...

  8. Appium Desktop介绍-xcodebuild failed with code 65 问题解决

    Appium Desktop介绍-xcodebuild failed with code 65  问题解决 一.Appium Desktop介绍 Appium Desktop是一款用于Mac.Wind ...

  9. Windows Server Core Command (管理服务器核心的具体操作命令)

    从 Windows Server 2008 开始,管理员可以选择安装具有特定功能但不包含任何不必要功能的 Windows Server 的最小安装服务器核心(Server Core),它为一些特定服务 ...

  10. (转)如何实现CSS限制字数,超出部份显示点点点...

    <div style="width:200px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis; border:1 ...