Python Object Oriented
1. Creating class
class className:
'Optional class documentation string'
class_suite
The class has a documentation string, which can be accessed via ClassName.__doc__.
Example:
class Employee:
'Common base class for all employees'
empCount = 0 def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
#class constructor or initialization method
#It's used to make a new class instance #the first argument to each method is self
#When we use this method, we do not need to add it. Python will adds the self argument to the list for you.
def displayCount(self):
print("Total Employee %d" % Employee.empCount) def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary) #create an instance
e = Employee('Ben', 1000)
2. access an attribute
just like C++
#We can add, remove, or modify attributes of classes and objects at any time.
emp1.age = 7 # Add an 'age' attribute.
emp1.age = 8 # Modify 'age' attribute.
del emp1.age # Delete 'age' attribute.
hasattr(emp1, 'age') # Returns true if 'age' attribute exists
getattr(emp1, 'age') # Returns value of 'age' attribute
setattr(emp1, 'age', 8) # Set attribute 'age' at 8
delattr(empl, 'age') # Delete attribute 'age'
3. Built-in class attribute
ictionary containing the class's namespace.
__name__ #Class name.
__module__ #Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__ #A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
4. destroy
#Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space.
'''
An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary).
The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope.
When an object's reference count reaches zero, Python collects it automatically.
'''
class Point:
def __init( self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "destroyed" pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3 #result
3083401324 3083401324 3083401324
Point destroyed
5. inheritance
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string'
class_suite
Python Object Oriented的更多相关文章
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- Python学习札记(三十) 面向对象编程 Object Oriented Program 1
参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...
- CSharpGL - Object Oriented OpenGL in C#
Object Oriented OpenGL in C#
- What is Object Oriented Design? (OOD)
Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...
- Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...
- Java - 面向对象(object oriented)计划 详细解释
面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...
- OO开发思想:面向对象的开发方法(Object oriented,OO)
面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
随机推荐
- Deep Reinforcement Learning 基础知识(DQN方面)
Introduction 深度增强学习Deep Reinforcement Learning是将深度学习与增强学习结合起来从而实现从Perception感知到Action动作的端对端学习的一种全新的算 ...
- HDU 2273
http://acm.hdu.edu.cn/showproblem.php?pid=2273 N辆车排队过马路,不能相撞,问最短时间 ans=车的总长度/最小速度 #include <iostr ...
- SQL查询执行步骤
1.总结 执行顺序 3.select ...聚合函数 from 表名 1.where ... 2.group by ... 4.having ... 5.order by ... 6.limit .. ...
- 自动化测试入门指南(3)-- 入门demo
按照 自动化测试入门指南(2)-- 环境搭建搭建好环境后,我们继续一步步实现一个简单的入门例子 Step0. 安装Firefox浏览器(http://pan.baidu.com/s/1c00bw8g中 ...
- java大文件断点续传
java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路:1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操 ...
- BZOJ4566 Haoi2016 找相同字符【广义后缀自动机】
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- xml的读取(曾删改)
获取XML 得到 需要查询的字段名 private string GetXml(string TableName) { try { string TbName = TableName.Split('_ ...
- 异步任务中的重新进入(Reentrancy)
一个按钮,点击执行一个任务.我们可能直接在它的 Click 事件中写下了执行任务的代码. 一般我们无需担心这样的代码会出现什么问题——但是,这样的好事情只对同步任务有效:一旦进入了异步世界,这便是无尽 ...
- 阿里云简单的HTTPS配置
1, 在CA证书服务中购买证书,填写信息,之后[域名授权配置]. 域名授权配置有两种,一种是文件配置,一种是DNS配置,这里我选择DNS配置. 记录类型:TXT 主机记录:按照上图填写 记录值:按照上 ...
- (转)GIL 与 Python 线程的纠葛
原文地址:http://zhuoqiang.me/python-thread-gil-and-ctypes.html 作者:Qiang GIL 与 Python 线程的纠葛 GIL 是什么?它对 py ...