python(python中的super函数、取整(int、round、ceil、modf)

学习目标:

Python学习十七、


学习内容:

1、python中的super函数
2、python取整(int、round、ceil、modf)


1、python中的super函数

在python中,类里的__init__方法(函数)和属性,在类实例化的时候会被自动执行
在类的继承当中可能覆盖同名的方法,这样就不能调用父类的方法了,只能调用子类的构造方法

  • super函数:调用父类或者超类的方法(函数)和属性
  • super(super所在的类名,self).属性或者父类(超类)的方法(函数)和属性:这是python2.x中的写法
  • super().属性或者父类(超类)的方法(函数)和属性:这是python3.x中的写法,同时兼容2.x的写法
class person():
    
    def __init__(self):
        print('你出生啦!!!')
    
    def say(self):
        print('hello the world!!!')

class student(person):

    def __init__(self,sex,name):
        super().__init__()
        self.sex = sex
        self.name = name
        print('%s %s你上学啦!!!'%(sex,name))

    def getName(self):
        return self.name

    def say(self):
        super().say()
        print('hello my school!!!')
st = student('man','小明')
print(st.getName())
st.say()

输出:
你出生啦!!!
man 小明你上学啦!!!
小明
hello the world!!!
hello my school!!!
  • super()函数调用父类的父类(超类)类跨过三代的方法(函数)和属性
class person():

    def __init__(self):
        print('你出生啦!!!')
    
    def say(self):
        print('hello the world!!!')
    
    def runs(self):
        print('you can run')

class student(person):
    
    def __init__(self,sex,name):
        self.sex = sex
        self.name = name

    def getName(self):
        return self.name

    def say(self):
        super().say()
        print('hello my school!!!')

class myrun(student):
    def runs(self):
        super().runs()
        print('you can run fast')

runss = myrun('man','xioaming')
runsss = runss.runs()

输出:
you can run
you can run fast

2、Python取整

1、向下取整

int()函数

a = 3.33
b = 3.56
print(int(a))
print(int(b))
输出:
3
3

2、四舍五入

round()函数

a = 3.33
b = 3.56
print(round(a))
print(round(b))
输出:
3
4

3、向上取整
需要引入math包

ceil()函数

import math
a = 3.33
b = 3.99
print(math.ceil(a))
print(math.ceil(b))
输出“
4
4

4、分别取出整数和小数部分
需要引入math包

modf()函数

import math
a = 3.33
b = 1.333333
print(math.modf(a))
print(math.modf(b))
输出:
(0.33000000000000007, 3.0)
(0.3333330000000001, 1.0)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值