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. 向量点积(Dot Product),向量叉积(Cross Product)

    参考的是<游戏和图形学的3D数学入门教程>,非常不错的书,推荐阅读,老外很喜欢把一个东西解释的很详细. 1.向量点积(Dot Product) 向量点积的结果有什么意义?事实上,向量的点积 ...

  2. runtime查找 UIAlertAction 的key 及 UIActionSheet 设置字体颜色

    修改不了颜色了 结果发现kvo 的key 不对 哎 直接上代码 设置正确的属性找到对应的key  还以为iOS 11改变了方法 unsigned int count; Ivar *ivars =  c ...

  3. 2D game engine essentials [to be continued...]

    All 2D Game Engines/Frameworks are trying to solve the same problem(s). Languages don't matter- they ...

  4. int('x', base)中的base参数

    >>> int('12', 16) 16表示'12'就是16进制数,int()要将这个16进制数转化成10进制.

  5. threejs 画二维圆(圆弧)

    画圆: var radius = 40, segments = 64, material = new THREE.LineBasicMaterial({ color: 0x0000ff }), geo ...

  6. 第一次Sprint团队贡献分

    201406114105       董婷婷           21 201406114157       容杰龙           22 201406114343       卓炜杰       ...

  7. CSU1632Repeated Substrings(后缀数组/最长公共前缀)

    题意就是求一个字符串的重复出现(出现次数>=2)的不同子串的个数. 标准解法是后缀数组.最长公共前缀的应用,对于样例aabaab,先将所有后缀排序: aab 3    aabaab 1    a ...

  8. 【BZOJ4025】二分图 LCT

    [BZOJ4025]二分图 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于 ...

  9. PEP

    用python ,  PEP难以绕过.  PEP是什么?   Python Enhancement Proposals , 它集合了python的改进提案. 它不是版本递进的, 有些PEP是应该去读一 ...

  10. vue踩坑记录

    一.element resetFields 方法报错 网上查的解决方案 resetForm(formName) { this.$nextTick(() => { this.$refs[formN ...