python class & inherit

简介:
类定义举例
class Human:
  # self有且必须有, 而且要放第一个参数的位置, 而且所有的方法也需要self放在第一位置.
  def __init__(self, v1, v2, v3):
    self.name = v1
    self.sex = v2
    self.age = v3
  def how_old(self):
    return(self.age)


实例化
>>> digoal=Human('digoal','male',32)
>>> digoal.name
'digoal'
>>> digoal.sex
'male'
>>> digoal.age
32
>>> digoal.how_old()
32
>>> digoal.age=100
>>> digoal.how_old()
100

使用dir()函数返回对象内的方法, 变量等.
>>> dir(digoal)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'how_old', 'name', 'sex']

不带参数输出的是当前的变量
>>> a=1
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']


输出变量类型
>>> type(digoal)
<class '__main__.Human'>

输出变量位置
>>> digoal
<__main__.Human object at 0x7fb4bf5aabe0>


继承
例如扩展list, 使之带name属性.
类名称中添加扩展类参数, 如下.
>>> class NamedList(list):
...   def __init__(self, v_name='',v_l=[]):
...     list.__init__([])      # 扩展类list初始化
...     self.name=v_name
...     self.extend(v_l)
... 
>>> a=NamedList('abc')
>>> a.name
'abc'

NamedList包含了list的所有变量和方法.
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove', 'reverse', 'sort']
>>> a.extend([1,2,3,4])
>>> a
[1, 2, 3, 4]
>>> a.reverse()
>>> a
[4, 3, 2, 1]


[其他]
1. Create a empty dictionary using the dict()  factory function or using {} .
创建空字典(类似json), 使用dict()构造函数, 或{}构造器
例子 : 
To access the value associated with the key Name in a dictionary called person, use the familiar square bracket notation: person['Name'] .
Like list and set, a Python's dictionary dynamically grows as new data is added to the data structure.
Populate a dictionary as you go: 
  new_d = {}  or new_d = dict() and then 
  d['Name'] = 'Eric Idle' 
    or do the same thing all in the one go: 
  new_d = {'Name': 'Eric Idle'}

2. The class keyword lets you define a class.
定义类, 使用class关键字, 一般类名称大写开头, 
继承类需要在类名称后加上继承类名作为参数例如
class NamedList(list):

3. Class methods (your code) are defined in much the same way as functions, that is, with the def keyword.
   Class attributes (your data) are just like variables that exist within object instances.
   The __init__()  method can be defined within a class to initialize object instances.
   Every method defined in a class must provide self as its first argument.
类方法定义和函数定义类似, 使用def method_name(self, 其他参数):
类方法必须使用self参数作为第一个参数.
__init__()用于创建实例时初始化实例.

4. Every attribute in a class must be prefixed with self.  in order to associate it data with its instance.
类的定义中, 所有类的属性都必须以self开头. 例如self.name

5. Classes can be built from scratch or can inherit from Python's built-in classes or from other custom classes.

6. Classes can be put into a Python module and uploaded to PyPI.
类可以放到模块中, 并upload到PyPI.

相关文章
|
4月前
|
API Python
python 详细理解 import ---- openstack自动import class 到 特定命名空间
python 详细理解 import ---- openstack自动import class 到 特定命名空间
46 0
|
11天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
34 0
|
1月前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
19 0
|
1月前
|
存储 设计模式 Python
Python中的类(Class)和对象(Object)
Python中的类(Class)和对象(Object)
30 0
|
8月前
|
数据挖掘 Linux Python
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
|
4月前
|
Kotlin Python
Python(三十一)python类class继承与多态
Python是一门面向对象语言 面向对象的三大特征:封装、继承、多态 我们这里来看一下python 类的继承 通过继承创建的新类称为子类或派生类,被继承的类称为基类、父类或超类。 一:类的继承 1 :继承语法 arduino 复制代码 class 派生类名(基类名) ... 通常我们定义的类没有指定继承对象都是继承自object类,另外,python是支持多继承的。 下边我们使用代码来演示一下python的继承: 我们定义一个person类,其中有name、age两个属性,getInfo方法,为父类。 我们定义一个school类,其中getSchoolInfo方法,为父类。 定义一
30 0
|
4月前
|
Kotlin Python
Python(三十)python中的类class
Python是一门面向对象语言。 Class类是面向对象中非常重要的一个元素 什么是类呢? Class类是一个功能的合集。 一:类的创建 1 :类的构造函数 python 复制代码 # 定义类 class tests: '测试创建类' # 类文档字符串 projectName='' def __init__(self,name): print("初始化类调用了构造函数") self.projectName = name print("构造函数接收到了参数:",self.projectName) T = tests('时间里
35 0
|
9月前
|
Python
Python Class 08-使用模块和标准库
Python Class 08-使用模块和标准库
|
9月前
|
Python
Python Class 07-再讲函数(闭包与递归)
Python Class 07-再讲函数(闭包与递归)
|
9月前
|
Python
Python Class 06-组合数据类型
Python Class 06-组合数据类型