官术网_书友最值得收藏!

Python classes

Python is an Object-Oriented Programming (OOP) language. The way Python creates objects are with the class keyword. A Python object is most commonly a collection of functions (methods), variables, and attributes (properties). Once a class is defined, you can create instances of such a class. The class serves as the blueprint of the subsequent instances.

The topic of object-oriented programming is outside the scope of this chapter, so here is a simple example of a router object definition:

    >>> class router(object):
... def __init__(self, name, interface_number,
vendor):
... self.name = name
... self.interface_number = interface_number
... self.vendor = vendor
...
>>>

Once defined, you are able to create as many instances of that class as you'd like:

    >>> r1 = router("SFO1-R1", 64, "Cisco")
>>> r1.name
'SFO1-R1'
>>> r1.interface_number
64
>>> r1.vendor
'Cisco'
>>>
>>> r2 = router("LAX-R2", 32, "Juniper")
>>> r2.name
'LAX-R2'
>>> r2.interface_number
32
>>> r2.vendor
'Juniper'
>>>

Of course, there is a lot more to Python objects and OOP. We will see more examples in the future chapters.

主站蜘蛛池模板: 拉萨市| 阿瓦提县| 尚志市| 尼勒克县| 视频| 合作市| 文水县| 花莲市| 石家庄市| 广西| 洛扎县| 苍梧县| 嘉禾县| 惠东县| 藁城市| 兴仁县| 黄冈市| 建始县| 永安市| 民勤县| 开化县| 景谷| 巴南区| 凉山| 林口县| 唐海县| 绥棱县| 五指山市| 河池市| 全州县| 宜城市| 宜州市| 吉安市| 泰宁县| 玉龙| 浦北县| 莒南县| 海门市| 神池县| 闸北区| 广汉市|