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的更多相关文章

  1. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  2. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

  3. Python学习札记(三十) 面向对象编程 Object Oriented Program 1

    参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...

  4. CSharpGL - Object Oriented OpenGL in C#

    Object Oriented OpenGL in C#

  5. What is Object Oriented Design? (OOD)

    Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...

  6. Python Object Graphs — objgraph 1.7.2 documentation

    Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...

  7. Java - 面向对象(object oriented)计划 详细解释

    面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...

  8. OO开发思想:面向对象的开发方法(Object oriented,OO)

    面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...

  9. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

随机推荐

  1. BitArray类的使用--(转换二进制数的内部实现过程)

    BitArray类用来处理位集合. 它和ArrayList十分类似,可以动态调整大小,可以在需要的时候添加二进制位而不用担心数组越界的问题.(所以本质它也是集合里套一个数组,可能是线性数组) 什么是位 ...

  2. MySQL 存储过程,游标,临时表创建

    -- -------------------------------------------------------------------------------- -- Routine DDL - ...

  3. Docker及常用操作

    镜像.容器和仓库 Docker镜像: 镜像是一个只读的模板,可以用来创建Docker容器.可以直接创建一个镜像,或者是更新已有镜像,或者复制他人的镜像直接使用. Docker容器: 容器是镜像的实例, ...

  4. Office for Mac

    前情 真的用不惯Mac上的pages,所以就找Mac版的office.找了一个发觉不对,安装总是出错,当时也没有太大的需求,就把这事搁置了下来.今天要写技术文档的时候才终于下决心要把office下再下 ...

  5. 【剑指offer】不用加减乘除做加法,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 第一步:不考虑进位对每一位相加(异或操作) 第二步:考虑进位(位与运算+左移) 第三步:第一步和第二步相加(重复执行前两步) # 代码 #include ...

  6. 让ASP.NET第一次请求不变慢

    网页开发者早期最常遇到一个问题,当网站布署到IIS时候,每当IIS或是Application Pool重启后,第一位进入网站的使用者,其体验往往第一个反应就是网站很慢,然后就一直询问IT为什么网站这么 ...

  7. 将 UWP 的有效像素(Effective Pixels)引入 WPF

    在很久很久以前,WPF 诞生之初,有一个神奇的单位,它的名字叫做——设备无关单位(DIP,Device Independent Unit).微软给它描绘了一片美好的愿景——在任何显示器上显示的尺寸是相 ...

  8. 接口测试框架——第三篇:发送(requests)

    把下面的代码放在requests_module.py文件中 # coding: utf-8 import requests import logging from requests.exception ...

  9. C#-StructLayoutAttribute(结构体布局)

    struct实例字段的内存布局(Layout)和大小(Size) 在C/C++中,struct类型中的成员的一旦声明,则实例中成员在内存中的布局(Layout)顺序就定下来了,即与成员声明的顺序相同, ...

  10. [CF895E]Eyes Closed

    luogu description 一个序列\(a_i\),支持一下两种操作. \(1\ \ l_1\ \ r_1\ \ l_2\ \ r_2\): 随机交换区间\([l_1,r_1]\)和\([l_ ...