Bootstrap

Python学习32天

Self

#比较两个人信息,完全相等输出True,否则输出False
class Person():
    name=None
    age=None
    def compare_to(self,other):
        return self.name==other.name and self.age==other.age
man1=Person()
man1.name="tim"
man1.age=3
man2=Person
man2.name="tim"
man2.age=2

print(man1.compare_to(man2))

class Person():
    name=None
    age=None
def f1(pp):
    pp.name="jj"
    pp.age+=1
man=Person
man.name="kk"
man.age=5
print(f"man.name:{man.name} man.age:{man.age} id :{id(man)}")
f1(man)
print(f"man.name:{man.name} man.age:{man.age} id :{id(man)}")
print(f"Person.name:{Person.name} Person.age:{Person.age}Person is id :{id(Person)}")

#在初始化对象时,会自动执行__int__方法

class Dog():
    name=None
    age=None
    # 构造方法
    def __init__(self,name,age):
        print(f"__int__执行了:{name,age}")

        print(f"self id:{id(self)}")
        self.name=name
        self.age=age
# 创建对象时直接赋值
dog1=Dog("hi",2)
print(f"dog1.name:{dog1.name} dog1.age:{dog1.age} dog1.id:{id(dog1)}")

;