以下示例是关于Python中包含在Python中创建类和对象用法的示例代码,想了解在Python中创建类和对象的具体用法?在Python中创建类和对象怎么用?在Python中创建类和对象使用的例子?那么可以参考以下相关源代码片段来学习它的具体使用方法。
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
本文地址:https://www.itbaoku.cn/snippets/785251.html