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

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.

主站蜘蛛池模板: 广灵县| 金堂县| 汤原县| 微山县| 阿拉善右旗| 黔东| 嘉鱼县| 龙口市| 建平县| 黄梅县| 神农架林区| 牡丹江市| 尤溪县| 宁夏| 盘锦市| 韶关市| 罗江县| 安徽省| 沙坪坝区| 体育| 吴忠市| 惠东县| 灵丘县| 吐鲁番市| 鄂托克前旗| 紫阳县| 桦川县| 广东省| 榕江县| 宿松县| 兴海县| 顺昌县| 丰台区| 邓州市| 肇东市| 九江市| 汉中市| 大田县| 庆云县| 乐东| 湘潭县|