- Python 3 Object Oriented Programming
- Dusty Phillips
- 469字
- 2021-08-03 15:36:06
Creating Python classes
We don't have to write much Python code to realize that Python is a very "clean" language. When we want to do something, we just do it, without having to go through a lot of setup. The ubiquitous, "hello world" in Python, as you've likely seen, is only one line.
Similarly, the simplest class in Python 3 looks like this:
class MyFirstClass: pass
There's our first object-oriented program! The class definition starts with the class
keyword. This is followed by a name (of our choice) identifying the class, and is terminated with a colon.
Note
The class name must follow standard Python variable naming rules (must start with a letter or underscore, can only be comprised of letters, underscores, or numbers). In addition, the Python style guide (search the web for "PEP 8"), recommends that classes should be named using CamelCase notation (start with a capital letter, any subsequent words should also start with a capital).
The class definition line is followed by the class contents, indented. As with other Python constructs, indentation is used to delimit the classes, rather than braces or brackets as many other languages use. Use four spaces for indentation unless you have a compelling reason not to (such as fitting in with somebody else's code that uses tabs for indents). Any decent programming editor can be configured to insert four spaces whenever the Tab key is pressed.
Since our first class doesn't actually do anything, we simply use the pass
keyword on the second line to indicate that no further action needs to be taken.
We might think there isn't much we can do with this most basic class, but it does allow us to instantiate objects of that class. We can load the class into the Python 3 interpreter so we can play with it interactively. To do this, save the class definition mentioned earlier into a file named first_class.py
and then run the command python
-i
first_class.py
. The -i
argument tells Python to "run the code and then drop to the interactive interpreter". The following interpreter session demonstrates basic interaction with this class:
>>> a = MyFirstClass() >>> b = MyFirstClass() >>> print(a) <__main__.MyFirstClass object at 0xb7b7faec> >>> print(b) <__main__.MyFirstClass object at 0xb7b7fbac> >>>
This code instantiates two objects from the new class, named a
and b
. Creating an instance of a class is a simple matter of typing the class name followed by a pair of parentheses. It looks much like a normal function call, but Python knows we're "calling" a class and not a function, so it understands that its job is to create a new object. When printed, the two objects tell us what class they are and what memory address they live at. Memory addresses aren't used much in Python code, but here, it demonstrates that there are two distinctly different objects involved.
- 中文版Premiere影視編輯課堂實錄
- Drupal Multimedia
- Cinema 4D電商美工與視覺設(shè)計案例教程(培訓(xùn)教材版)
- Jetpack Compose:Android全新UI編程
- PostgreSQL 9.0 High Performance
- 無師自通AutoCAD 2014中文版機(jī)械設(shè)計
- Quality Assurance for Dynamics AX-Based ERP Solutions
- Photoshop后期強(qiáng):多重曝光專業(yè)技法寶典
- Photoshop & Illustrator平面設(shè)計火星課堂
- 攝影師的后期必修課(RAW格式篇)
- OpenAM
- 攝影師的后期課:Lightroom后期技法篇
- SolidWorks 2020中文版入門、精通與實戰(zhàn)
- UG NX 9.0模具設(shè)計工廠實訓(xùn)
- Photoshop實例教程:Photoshop 2022(電子活頁微課版·第3版)