python继承函数重载 继承的函数可以重载吗

关于python的重载问题

没有重载,但是可以有默认参数和不定长参数,可以判断默认值和参数长度来处理。

我们提供的服务有:成都网站制作、网站建设、外贸网站建设、微信公众号开发、网站优化、网站认证、志丹ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的志丹网站制作公司

比如:

def range(start, end = -1):

if end == -1:

end = start

start = 0

def range(*args):

if len(args) == 1:

start = 0

end = args[0]

elif len(args) == 2:

start, end = args[0], args[1]

为什么 Python 不支持函数重载

python是动态脚本型语言,设计的机制就是按函数名来保存函数入口,而不是函数名+参数。

你调用函数的时候,可以多传参数,一样的会调用到只是函数名匹配的那个函数。

python虽然不支持函数重载,但是可以通过传递容器类型的参数(list、tuple、set、dict)来实现类似的功能

如何解决python2不能隐式继承

继承是所有开发语言的必修内容,而本文写的只是Python继承中的特殊之处,关于继承概念及内容可以自行百度(不装B,感觉百度挺好的)1.构造函数:

要说继承,先要说一下构造函数。Java要求是与类名相同并且无返回值,而Python则是强制要求命名为“__init__()”。

当创建类的对象时,会自动先调用构造函数,一般用于初始化。构造函数可以不写,那么程序会隐式自动增加一个空的构造函数。

2.继承写法:

(1).class 空格 类名称 括号内填写父类名 冒号具体写法如下class A:

def __init__(self):

pass

def print_class_name(self):

print "this is class A"

class B(A):

def __init__(self):

pass

if __name__ == "__main__":

class_b = B()

class_b.print_class_name()

上面代码首先定义了一个名为“A”的类,包含一个名为“print_class_name”的方法。然后,定义一个名为“B”的类,继承“A”,同时继承了“A”类的“print_class_name”的方法。

此时“A”类为“B”类的父类或者叫基类,“B”类是“A”类的子类,子类会继承父类的所有公共方法。

(2).意义:

一字记之曰“懒!”(感叹号不算字)我始终相信赖人才能推动科学进步。

言归正传,假如你要写一个老王类,包含年龄、性别等方法,后面还要写一个老王的儿子小王类,也有年龄、性别等方法?

class FatherWang:

def __init__(self, age=43, sex='man'):

self.a = age

self.s = sex

def age(self):

print self.a

def sex(self):

print self.s

class SonWang:

def __init__(self, age=13, sex='man'):

self.a = age

self.s = sex

def age(self):

print self.a

def sex(self):

print self.s

if __name__ == "__main__":

father = FatherWang(43, "man")

father.age()

father.sex()

son = SonWang(13, "man")

son.age()

son.sex()

你会发现两个类中有相同名称和功能的方法,这样写岂不是很重复很累?(尽管按键盘次数不算太多,我依然觉得很累)如果有继承就很好解决了。

class FatherWang:

def __init__(self, age=43, sex='man'):

self.a = age

self.s = sex

def age(self):

print self.a

def sex(self):

print self.s

class SonWang(FatherWang):

def __init__(self, age=13, sex='man'):

FatherWang.__init(age, sex)

if __name__ == "__main__":

father = FatherWang(43, "man")

father.age()

father.sex()

son = SonWang(13, "man")

son.age()

son.sex()

两者运行结果完全一样,但是使用继承方法却省了很多按键盘的次数。

3.经典类与新式类:

(1)经典类写法:

class A:

pass

(2)新式类写法:

class A(object):

pass

可以看出,新式类和经典类的区别在于,是否继承object这个基类。object是所有类的父类。所以之前不带“(object)”的写法,属于经典类写法,加上“(object)”就是新式类的写法。

(3).原因:这里我得吐槽一下Python的版本混乱。2.2版本之前只有经典类写法,这里有一个问题,代码如下?

class A:

pass

class B(object):

pass

a = A()

b = B()

print a.__class__

print type(a)

print "----------"

print b.__class__

print type(b)

结果为:

__main__.A

type 'instance'

----------

class '__main__.B'

class '__main__.B'

首先A类为经典类,B类为新式类。__class__属性和type()方法都是返回对象类型,那么问题来了,使用经典类的写法返回结果却不一致。因此在2.2版本之后出现了新式类来解决这个问题,自然,新式类和经典类还有更大的区别在后面说。另外在3.3版本中,无论使用哪种写法,python都会隐式的继承object,所以3.3版本不会再有经典类(在这里我只想问,早干什么去了!),但是鉴于3.3兼容性问题,貌似没有太多人用。

4.方法重写与方法重载

(1).方法重写:

class FatherWang:

def __init__(self, age=43, sex='man'):

self.a = age

self.s = sex

def age(self):

print self.a

def sex(self):

print self.s

def name(self):

print "Wang_yang"

class SonWang(FatherWang):

def __init__(self, age=13, sex='man'):

FatherWang.__init(age, sex)

def name(self):

print "Wang_xiaoming"

if __name__ == "__main__":

father = FatherWang(43, "man")

father.age()

father.sex()

father.name()

son = SonWang(13, "man")

son.age()

son.sex()

son.name()

比继承写法(2)中的代码相比,两个类分别多了同名的方法“name”,之前说过子类会继承父类的方法,那么这时候两个类有相同名字的方法,冲突了,怎么处理?

这个时候,就叫方法重写。可以理解为,子类的“name”方法把父类的“name”方法覆盖了,重新写了,所以调用子类的“name”方法时,会以子类的为准(尽管这种理解并不准确,但是可以很好解释“方法重写”这个名词,后面会讲到正确理解)。

注意下面的代码

class FatherWang:

def __init__(self, age=43, sex="man"):

self.a = age

self.s = sex

print "I am FatherWang"

def age(self):

print "Father age:"+str(self.a)

def sex(self):

print "Father sex:"+str(self.s)

class MotherLi:

def __init__(self, age=40, sex="woman"):

self.a = age

self.s = sex

print "I am MotherLi"

def age(self):

print "Mother age:"+str(self.a)

def sex(self):

print "Mother sex"+str(self.s)

class SonWang(FatherWang, MotherLi):

def __init__(self, age=13, sex="man"):

FatherWang.__init__(self, age, sex)

MotherLi.__init__(self, age, sex)

print "I am SonWang"

if __name__ == "__main__":

son = SonWang()

son.age()

son.sex()

执行结果:

I am FatherWang

I am MotherLi

I am SonWang

Father age:13

Father sex:man

在之前代码上稍作修改,另外增加了一个MotherLi的类,SonWang类继承了FatherWang类和MotherLi类。注意,这是经典类的写法。

首先,我们知道了python多继承的写法,就是在括号中上一个父类后面加个逗号,然后再写上下一个父类的名字:

class SonWang(FatherWang, MotherLi):

其次,FatherWang类和MotherLi类,都有名为age和sex方法,SonWang类为什么会继承FatherWang类的方法呢?那么把SonWang类的继承顺序改一下class SonWang(MotherLi, FatherWang):

就会发现继承的是MotherLi类的方法。

通过结果可知,是按照继承的顺序。

让我们把代码结构变得更发杂一些吧,我想会崩溃的,哈哈哈?

class Grandfather:

def __init__(self, age=73, sex="man"):

self.a = age

self.s = sex

print "I am Grandfather"

def age(self):

print "Grandfather age:"+str(self.a)

def sex(self):

print "Grandfather sex:"+str(self.s)

def Interesting(self):

print "Grandfather Interesting"

class Grandmother:

def __init__(self, age=70, sex="woman"):

self.a = age

self.s = sex

print "I am Grandmother"

def age(self):

print "Grandmother age:"+str(self.a)

def sex(self):

print "Grandmother sex:"+str(self.s)

def Interesting(self):

print "Grandmother Interesting"

class FatherWang(Grandfather, Grandmother):

def __init__(self, age=43, sex="man"):

self.a = age

self.s = sex

Grandfather.__init__(self, age, sex)

Grandmother.__init__(self, age, sex)

print "I am FatherWang"

def age(self):

print "Father age:"+str(self.a)

def sex(self):

print "Father sex:"+str(self.s)

class MotherLi(Grandfather, Grandmother):

def __init__(self, age=40, sex="woman"):

self.a = age

self.s = sex

Grandfather.__init__(self, age, sex)

Grandmother.__init__(self, age, sex)

print "I am MotherLi"

def age(self):

print "Mother age:"+str(self.a)

def sex(self):

print "Mother sex"+str(self.s)

def Interesting(self):

print "MotherLi Interesting"

class SonWang(FatherWang, MotherLi):

def __init__(self, age=13, sex="man"):

FatherWang.__init__(self, age, sex)

MotherLi.__init__(self, age, sex)

print "I am SonWang"

if __name__ == "__main__":

son = SonWang()

son.age()

son.sex()

son.Interesting()

执行结果:

I am Grandfather

I am Grandmother

I am FatherWang

I am Grandfather

I am Grandmother

I am MotherLi

I am SonWang

Father age:13

Father sex:man

Grandfather Interesting

话说,我自己都有点儿晕。简单来讲,就是儿子继承了老爸、老妈,然后老爸继承了爷爷、奶奶,妈妈继承了老爷、姥姥。(真是一大家子啊)通过执行结果可知,儿子类先找到老爸类,然后再找老爸类的第1个父类爷爷类,此时发现爷爷类没有父类了,那么执行初始化。然后还要继续找到老爸类的第2个父类奶奶类,此时发现奶奶类没有父类了,执行初始化。此时老爸类的所有父类都初始化完成,初始化自己。然后开始找妈妈类……那么为什么Interesting方法会使用爷爷类的呢?奶奶类、老爷类、姥姥类都有啊?首先儿子类没有Interesting方法,会先找第1个父类老爸类。发现老爸类也没有,再找老爸类的第1个父类,发现找到了,那么就直接调用不再往下找了。

结论:经典类的多继承,按照继承顺序查找。即,从左到右,从下到上的方式。注意,只有经典类是这样的!

(2).新式类的多继承:

class Grandfather(object):

def __init__(self, age=73, sex="man"):

self.a = age

self.s = sex

print "I am Grandfather"

def age(self):

print "Grandfather age:"+str(self.a)

def sex(self):

print "Grandfather sex:"+str(self.s)

def Interesting(self):

print "Grandfather Interesting"

class Grandmother(object):

def __init__(self, age=70, sex="woman"):

self.a = age

self.s = sex

print "I am Grandmother"

def age(self):

print "Grandmother age:"+str(self.a)

def sex(self):

print "Grandmother sex:"+str(self.s)

def Interesting(self):

print "Grandmother Interesting"

class FatherWang(Grandfather, Grandmother):

def __init__(self, age=43, sex="man"):

self.a = age

self.s = sex

Grandfather.__init__(self, age, sex)

Grandmother.__init__(self, age, sex)

print "I am FatherWang"

def age(self):

print "Father age:"+str(self.a)

def sex(self):

print "Father sex:"+str(self.s)

class MotherLi(Grandfather, Grandmother):

def __init__(self, age=40, sex="woman"):

self.a = age

self.s = sex

Grandfather.__init__(self, age, sex)

Grandmother.__init__(self, age, sex)

print "I am MotherLi"

def age(self):

print "Mother age:"+str(self.a)

def sex(self):

print "Mother sex"+str(self.s)

def Interesting(self):

print "MotherLi Interesting"

class SonWang(FatherWang, MotherLi):

def __init__(self, age=13, sex="man"):

FatherWang.__init__(self, age, sex)

MotherLi.__init__(self, age, sex)

print "I am SonWang"

if __name__ == "__main__":

son = SonWang()

son.age()

son.sex()

son.Interesting()

执行结果:

I am Grandfather

I am Grandmother

I am FatherWang

I am Grandfather

I am Grandmother

I am MotherLi

I am SonWang

Father age:13

Father sex:man

MotherLi Interesting

python中的super是什么?-

目录

python中的super,名为超类,可以简单的理解为执行父类的__init__函数。由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题。那么本文就着重看下super的具体作用。

通过设计这样一个案例,我们可以明确super的前后逻辑关系:先定义一个父类 initial ,在这个父类中有参数值 param 和函数 func ,然后用子类 new 来继承父类 initial 。继承之后,在子类的 __init__ 函数中 super 执行的前后去打印参数值 param 和函数 func 的返回值,相关代码如下所示:

代码的执行结果如下所示:

首先我们注意到,父类 initial 中的 __init__ 函数内的打印语句,是在super之后才输出的,这说明了, super 函数是在执行父类的初始化操作。那么如果没有执行 super , new 子类对 initial 父类的继承体现在哪里呢?答案就是父类的成员函数,比如这样的一个案例:

其实就是删掉了子类中重载的成员函数,那么得到的结果如下:

可以发现在执行super之前就可以打印父类的 func 函数的函数值。所以python中继承的逻辑是这样的:

initial.func() new.__init__() new.func()/new.param super() initial.__init__()/initial.param new.__init__()/new.paraminitial.func() new.__init__() new.func()/new.param super() initial.__init__()/initial.param new.__init__()/new.param

也正是因为只有执行了 super 才能初始化父类中的成员变量,因此如果在super之前是无法访问父类的成员变量的。

本文通过一个python的实际案例的设计,来讲解python面向对象的技术——类的继承中必用的super函数的逻辑。其实我们可以把python中类的继承理解成这样的一个过程:当我们在括号中明确了父类时,其实已经引用了父类的成员函数,但是并没有执行父类的初始化函数。在执行子类的初始化函数的同时,会检查是否重载了父类的成员函数,如果重载则会直接覆盖。而只有在执行了super之后,才相当于执行了父类的初始化函数,此时才可以访问父类的成员变量。

本文首发链接为:

作者ID:DechinPhy

更多原著文章请参考:

打赏专用链接:

腾讯云专栏同步:

python类的继承和单例模式 singleton、运算符重载

@[toc]

全局只有一个实例

font color=#03a3e3 该实现方式在多线程场景下不安全

继承其他类的类称为派生类(derived class)

被其他类继承的类称为这些类的基类(base

class)

需要注意圆括号中基类的顺序:font color=#03a3e3 从左到右搜索 font

多继承会导致菱形 diamond关系:有至少一个基类可以从子类经由多个继承路径到达

基类方法可能被多次调用

防止重复访问,每个基类只调用一次

通过子类实例对象课调用父类已被覆盖

慎用多继承(二义性)

– 对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

– 运算符重载不能改变其本来寓意

– 运算符重载只是一种 “语法上的方便” (sugar)

– 是一种函数调用的方式

python 重载方法有哪些特点

说起python重载,大家学过其他的语言比如说C#语言的话,应该知道有方法重载和运算符重载的概念,但是python重载的话,有它自己的特殊性,下面我说说自己的一点看法,希望大家踊跃拍砖。python 的重载主要包括方法重载和运算符重载。1.python 方法重载: 其他的语言一般对于方法重载的话,主要是根据参数的类型不同或者是数量不同来区分同名的方法。而python则比较特殊,它本身是动态语言,方法的参数是没有类型的,当调用传值的时候才确定参数的类型,故对参数类型不同的方法无需考虑重载。对参数数量不同的方法,则(大多数情况下)可以采用参数默认值来实现。比如你可以定义函数的默认值:def info(x,y,z=1): pass2.python 运算符重载: 在C#中,我们通过使用关键字operator定义一个运算符方法,并定义与所在类相关的运算符行为。在 Python中,运算符重载的方式更为简单——每一个类都默认内置了所有可能的运算符方法,只要重写这个方法,就可以实现针对该运算符的重载。例如以下是重载加法操作:class Info(object): def __init__(self): self.a = 11 self.b = 22 def __add__(self,x): return self.a * self.b 上面的例子是重写了+操作符号,你也可以重载其他的运算符。比如你可以重载乘号运算符,感兴趣的话,可以自己写写代码。希望上面讲的2点能够让你对python重载有个简单的认识。有兴趣的可以关注下。


分享文章:python继承函数重载 继承的函数可以重载吗
新闻来源:http://pwwzsj.com/article/hhoehd.html