栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python细碎知识点(getattr......)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python细碎知识点(getattr......)

类外给类增加属性、设置属性
class change: 
    def __init__(self, x, y, z): 
        self.a = x + y + z 
x = change(1,2,3) 
y = getattr(x, 'a') 
print(y)
y = getattr(x, "b", 1) # 第三个参数是默认值,不存在该属性则将y设置为默认值,
# 注意y是int型数据!!!
print(y)
setattr(x, 'a', y+1)
print(x.a)
# 6
# 1
# 2
类外定义属性 
class fruits:
    def __init__(self, price):
        self.price = price
obj=fruits(50)
obj.quantity=10
obj.bags=2
print(obj.quantity+len(obj.__dict__))
# 13
Python运算符重载: 
Suppose i is 5 and j is 4, i * j is same as ______.
A. i.__mul(j)
B. i.__mul__(j)
C. i.__Mul(j)
D. i.__MUL(j)
我的答案: B
If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.
A. obj.__str__()
B. str(obj)
C. print(obj)
D. all of the mentioned
我的答案: D
# 实现自己的复数类型,运算符重载
class MyComplex:
    def __init__(self,real,image):
        self.real = real
        self.image = image
    def __add__(self,other):
        return MyComplex(self.real+other.real,self.image+other.image)
    def __sub__(self,other):
        return MyComplex(self.real-other.real,self.image-other.image)
    def __mul__(self, other):
        re = self.real*other.real-self.image*other.image
        im = self.real*other.real-self.image*other.image
        return MyComplex(re,im)
    def __str__(self):
        if self.image>0:           
            return str(self.real)+'+'+str(self.image)+'j'
        else:
            return str(self.real)+str(self.image)+'j'
def main():
    c1 = MyComplex(1,2)
    print(c1)
    c2 = MyComplex(-2,3)
    for i in range(3):
        op = input()
        if op == '+':
            print(c1+c2)
        elif op == '-':
            print(c1-c2)
        elif op == '*':
            print(c1*c2)
        else:
            break
if __name__=="__main__":main()

 

类的方法__doc__(输出注释) 
# What will be the output of the following Python code?
class stud:
    'Base class for all students'
    def __init__(self, roll_no, grade):
        self.roll_no = roll_no
        self.grade = grade
    def display (self):
        print("Roll no : ", self.roll_no, ", Grade: ", self.grade)
print(stud.__doc__)

A. Exception is thrown
B. __main__
C. Nothing is displayed
D. Base class for all students
我的答案: D
类的方法__name__: 
What does print(Test.__name__) display (assuming Test is the name of the class)?
A. ()
B. Exception is thrown
C. Test
D. __main__
我的答案: C
变量作用域: 
def add(c,k): 
    c.test += 1 
    k += 2 
class A: 
    def __init__(self): 
        self.test = 0 
a=A() 
k=0 
add(a,k) 
print(c.test, k) 

A. 0 0
B. 1 0
C. 0 2
D. 1 2
我的答案: B
 set的详细用法
s = set() # 将集合初始化为空的
# 不能是 s={},这里s树dict类型

​​​​​​​Python基本数据类型之set - morra - 博客园 (cnblogs.com)https://www.cnblogs.com/whatisfantasy/p/5956775.html

Generation
def fib(x):
    n,a,b=0,0,1
    while n 

生成器 - 廖雪峰的官方网站研究互联网产品和技术,提供原创中文精品教程https://www.liaoxuefeng.com/wiki/1016959663602400/1017318207388128Generators in Python - GeeksforGeekshttps://www.geeksforgeeks.org/generators-in-python/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号