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

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.

主站蜘蛛池模板: 石台县| 平谷区| 西盟| 铅山县| 京山县| 海南省| 红安县| 新邵县| 徐州市| 怀仁县| 土默特右旗| 疏勒县| 万宁市| 广平县| 长宁县| 卢湾区| 太保市| 子洲县| 建瓯市| 子洲县| 阜宁县| 名山县| 金寨县| 长沙县| 阳春市| 平山县| 济宁市| 武隆县| 米林县| 玉田县| 历史| 忻州市| 仙居县| 和龙市| 泉州市| 正蓝旗| 永嘉县| 宁海县| 永新县| 浠水县| 巢湖市|