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 ...
随机推荐
- Scikit-learn方法使用总结
在机器学习和数据挖掘的应用中,scikit-learn是一个功能强大的python包.在数据量不是过大的情况下,可以解决大部分问题.近期在学习使用scikit-learn的过程中,我自己也在补充着机器 ...
- iOS-Core Data 详解
使用Core Data 框架 Core Data框架本质就是一个ORM(对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一 ...
- mysql安装优化
安装centos7 安装mysql解决方案1 安装mariadb解决方案2 官网下载安装mysql-server时区批量配置编码远程连接my.cnf简版的参数说明核心配置参数升级版mysql存储引擎M ...
- Servlet实现文件上传和下载
对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具commo ...
- BZOJ3566 SHOI2014 概率充电器 【概率DP】
BZOJ3566 SHOI2014 概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器: “采用全新纳米级加工技术,实现元件与导线能 ...
- 云设计模式-Design patterns for microservices
云设计模式 https://azure.microsoft.com/zh-cn/blog/design-patterns-for-microservices/ https://www.cnblogs. ...
- B/S供应链(打印管理)问题答疑
1.环境上的搭建答疑 .{"System.Data.OracleClient需要 Oracle 客户端软件 version 8.1.7或更高版本."} 解决:未装客户端,或所装版本 ...
- pyqtree
pyqtree module API Documentation Classes class Index The top spatial index to be created by the user ...
- Tornado部署与运行
运行多个Tornado实例 网页响应不是特别的计算密集型处理多个实例充分利用 CPU多端口怎么处理4.使用Supervisor监控Tornado进程安装(注意看是否需要指定使用python2版本) s ...
- springboot: 集成freemark模板引擎
1.freemark简介(摘自:http://blog.csdn.net/liaomin416100569/article/details/78349072) 在互联网软件内容网站中 一般首页的访问量 ...