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. 【Scipy】初步认识

    Scipy扩展包括多种多样的工具箱,这些工具致力于解决科学计算中的常见问题.不同的子模块对应不同的应用,比如插值, 整合, 优化, 图像处理, 统计, 特殊功能等等. scipy可以和其他的标准科学计 ...

  2. link @import区别 src href的区别

    先说页面引入css的四种方式吧 1 在头部写在style里面 2 行内样式 tyle= 3 外部引入 link和@import的区别 link属于XHTML的标签,而@import只是css提供的一种 ...

  3. bzoj3901

    题解: 就是按照常规的合并 期望有一点麻烦 首先计算全部的和 再减去有多少种 具体看看http://blog.csdn.net/PoPoQQQ/article/category/2542261这个博客 ...

  4. 在jenkins和sonar中集成jacoco(二)--在jenkins中生成jacoco覆盖率报告

    先要在jenkins上安装jacoco的插件,安装完成之后在job的配置项中可以增加这个选项: 第一个录入框是你的覆盖率文件(exec),第二个是class文件目录,第三个是源代码文件目录. 配置好了 ...

  5. eclipse 编码设置【转】

    一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样以后新建的文件也是新设置的编码格式. eclipse->window->p ...

  6. 通过java解析域名获得IP地址

    IP地址是Internet主机的作为路由寻址用的数字型标识,人不容易记忆.因而产生了域名(domain name)这一种字符型标识. DNS即为域名解析服务.在这里我们如果想通过java程序来解析域名 ...

  7. ubuntu 11.04 old sources.list

    #deb cdrom:[Ubuntu 11.04 _Natty Narwhal_ - Release amd64 (20110427.1)]/ natty main restricted # See ...

  8. 线上服务器TCP被打满是啥情况

    从一个线上服务器警告谈谈backlog https://wangxiangnan.cc/?p=105 缘起 双十一如期而至,此时的我因为在处理客户的一个问题已经陷入了忙碌.突然,不断接到驻场实施发来的 ...

  9. numpy pandas matplotlib

    import numpy as np import pandas as pd import matplotlib.pyplot as plt ---------------numpy--------- ...

  10. ringojs 基于jvm 的javascript 平台试用

    ringojs 是一个基于jvm 的javascript 平台,支持commonjs 模块模式 安装 下载包配置环境变量,或者使用docker,测试使用docker dockerfile deb 包安 ...