运行下面一段就成功
class Animal: pass
dog = Animal()
cat = Animal()
dog.name = 'xiaohong'
dog.age = 13
cat.name = 'xiaohong2'
cat.age = 14
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
class Animal: pass
dog = Animal()
cat = Animal()
dog.name = 'xiaohong'
dog.age = 13
cat.name = 'xiaohong2'
cat.age = 14
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
2021-12-16
__call__魔法方法可以将类当作函数来使用,当作为函数来使用时,调用call方法。
例如 文中的 p("Alice"),即相当于之行p.__call__("Alice")
例如 文中的 p("Alice"),即相当于之行p.__call__("Alice")
2021-12-08
def f(x):
return x.title()
l = []
for item in map(f, ['alice', 'BOB', 'CanDY']):
l.append(item)
print(l)
return x.title()
l = []
for item in map(f, ['alice', 'BOB', 'CanDY']):
l.append(item)
print(l)
2021-12-01
# Enter a code
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
dog = Animal('Peide Wang', 19)
cat = Animal('Wozhou Wu', 18)
print(dog.name)
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
dog = Animal('Peide Wang', 19)
cat = Animal('Wozhou Wu', 18)
print(dog.name)
2021-10-09
class Animal(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.count = Animal.count + 1
cat = Animal('lucy', 10)
dog = Animal('lily', 2)
dog2 = Animal('lily', 2)
cat2 = Animal('lily', 2)
print(Animal.count)
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.count = Animal.count + 1
cat = Animal('lucy', 10)
dog = Animal('lily', 2)
dog2 = Animal('lily', 2)
cat2 = Animal('lily', 2)
print(Animal.count)
2021-09-07
class Animal(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
#dog = Animal('diandian', 8)
#cat = Animal('mimi', 3)
Animal.count = Animal.count + 1
dog = ("doudou", 3)
print(Animal.count)
cat = ("diandian",4)
print(Animal.count)
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
#dog = Animal('diandian', 8)
#cat = Animal('mimi', 3)
Animal.count = Animal.count + 1
dog = ("doudou", 3)
print(Animal.count)
cat = ("diandian",4)
print(Animal.count)
2021-09-03
class Anaml():
pass
def _init_(self,name,age)
self.name = name
self.age = age
dog = Anmial('diandian ',8)
cat = Anmial('mimi',5)
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
pass
def _init_(self,name,age)
self.name = name
self.age = age
dog = Anmial('diandian ',8)
cat = Anmial('mimi',5)
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
2021-09-03
class Animal:
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
2021-09-01
# Enter a code
class Animal():
pass
dog=Animal()
cat=Animal()
dog.age=8
dog.name='jiji'
print(dog)
print(dog.name)
class Animal():
pass
dog=Animal()
cat=Animal()
dog.age=8
dog.name='jiji'
print(dog)
print(dog.name)
2021-09-01