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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值