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. vue-router与v-if实现tab切换的思考

    vue-router 该如何使用 忽然碰到一个常见的问题,明明可以使用 v-if / v-show 可以的解决的问题,有没有必要是使用 vue-router来解决. 比如常见的 tab 切换.一时间, ...

  2. POJ 1321 棋盘问题 dfs 难度:0

    http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...

  3. fcntl 改变描述符属性

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  4. Python 编程核心知识体系-函数(二)

    函数

  5. linux提权辅助工具(三):privchecker.py

    来自:https://www.securitysift.com/download/linuxprivchecker.py #!/usr/env python ##################### ...

  6. 安装xadmin后台管理插件

    django自带的admin后台管理功能太少.使用国人开发的xadmin后台,使用pip install xadmin安装在线包时,会出错,其中的README.rst是utf8格式,我们win7系统默 ...

  7. .NET工具软件收集

    ======================== ILSPY        官网:  http://ilspy.net/ .NET Reflector                官网:http:/ ...

  8. 【MySQL案例】ERROR 1786 (HY000)

    1.1.1. ERROR 1786 (HY000) [环境描写叙述] msyql5.6.14 [报错信息] 运行create table ... select的时候遇到报错: db1 [test] [ ...

  9. An internal error occurred during: "Android Library Update".

    打开 proj.android下的.cproject文件,把多余的CConfiguration配置节删掉,只留第一个就行了.

  10. unity的sprite添加点击事件

    直接说方法 添加一个2dxxx的碰撞器 添加一个OnMouseDown的回调函数,这个函数看script reference就可以