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


目录

类外给类增加属性、设置属性

类外定义属性 

Python运算符重载: 

类的方法__doc__(输出注释) 

类的方法__name__: 

变量作用域: 

 set的详细用法

Generation

字符串是不可变类型

类外给类增加属性、设置属性

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<x:
        yield b
        a,b=b,a+b
        n+=1
    return b
# print()
for i in fib(9):
    print(i)
# 1
# 2
# 3
# 5
# 8
# 13
# 21
# 34

生成器 - 廖雪峰的官方网站研究互联网产品和技术,提供原创中文精品教程

https://www.liaoxuefeng.com/wiki/1016959663602400/1017318207388128
Generators in Python - GeeksforGeeks

https://www.geeksforgeeks.org/generators-in-python/

字符串是不可变类型

('ab', 'cd', 'efcdgh')
>>> x='abcd'
>>> a=x.upper()
>>> x
'abcd'
>>> a
'ABCD'
>>>

 


文章作者: AllenMirac
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 AllenMirac !
  目录