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. gzip压缩初探

    前言 我们平时工作中传文件时为了提高传输速度一般都会把文件压缩一下再传,那样速度回快一些,尤其是那些文件很多的文件夹,比较常用的压缩格式就是zip,rar了.那我们日常网页中利用http协议请求的那些 ...

  2. SpringMvc中@ModelAttribute注解的使用

    一.绑定请求参数到指定对象 public String test1(@ModelAttribute("user") UserModel user) 只是此处多了一个注解@Model ...

  3. Linux修改字符集

    set NLS_LANG=american_america.AL32UTF8-----Linux下查看及更改oracle字符集编码[root@OracleDB ~]# cd /usr/local/or ...

  4. Android sdk 更新后编译不过,【Could not find com.android.sdklib.build.ApkBuilderMain】

    最近更新了Android sdk,发现编译不过了 解决方案: 进入 sdk/tool/lib/ 目录下,看看有没有 sdklib.jar 这个文件,如果没有看看有没有sdklib-25.*.*.jar ...

  5. 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationT

    严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationT (2012 ...

  6. 阿里云windows时间同步服务地址

    偶然发现的, 记录一下 ntp1.aliyun.com

  7. 最佳C/C++编辑器 source insight3

    C/C++嵌入式代码编辑器source insight3下载地址 http://www.sourceinsight.com/eval.html 注册码:SI3US-361500-17409

  8. [Qt] QLineEdit 仿QQ签名框

    今天鼓捣了半天,终于实现了自定义Qt中的QlineEdit控件的大致效果. 这个问题对于新手而言,主要有以下几个难点: 1.继承QLineEdit控件 2.QSS设置QLineEdit的相关样式,可以 ...

  9. iperf/iperf3网络测试工具的安装与使用

    1.官网及下载路径: iperf3 homepage at: http://software.es.net/iperf/Report bugs to: https://github.com/esnet ...

  10. 初识安卓小程序(Android短信发送器)

    首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"短信发送器" 然后在res目录下找到layout目录,找到activity_main.xml或fragment_mai ...