- Mastering Python Networking
- Eric Chou
- 172字
- 2021-07-02 21:42:33
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.
推薦閱讀
- Node.js+Webpack開發實戰
- Raspberry Pi for Secret Agents(Third Edition)
- 營銷數據科學:用R和Python進行預測分析的建模技術
- Implementing Cisco Networking Solutions
- JavaScript by Example
- 深入淺出RxJS
- MySQL數據庫基礎實例教程(微課版)
- Serverless computing in Azure with .NET
- Create React App 2 Quick Start Guide
- iPhone應用開發從入門到精通
- Node.js開發指南
- C++從入門到精通(第6版)
- PHP與MySQL權威指南
- 深入解析Java編譯器:源碼剖析與實例詳解
- Python預測分析與機器學習