Python Classes/Objects | |||
| |||
Python is object-oriented. In Python, most things are objects with properties and methods. Class is an object constructor or "blueprint" for creating objects. Create a Class To create a class, use the keyword
Create Object Now we can use the class named MyClass to create objects:
The __init__() Function The examples above are classes and objects in their simplest form, and are not really useful in real life applications. To understand the meaning of classes we have to understand the built-in __init__() function. All classes have a function called __init__(), which is always executed when the class is being initiated. Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
The __str__() Function The __str__() function controls what should be returned when the class object is represented as a string. If the __str__() function is not set, the string representation of the object is returned:
Example Class
Object Methods Objects can have methods. Methods are object-specific functions. Let us create a method in the Person class:
The self Parameter The It does not have to be named
Modify Object Properties You can modify properties on objects like this:
Delete Object Properties You can delete properties on objects by using the
Delete Objects You can delete objects by using the
The pass Statement
Tags: Python Python Classes Python Objects | |||
| |||
| |||
|
OTHER POSTS IN THE SAME CATEGORYMastering Excel Data Manipulation with PythonTry...Catch Helps Ignoring Data Type Miss-Match Error in PythonRegExp example in Python to exclude javascript from HTML codePython code to convert from Lunar to SolarPython example to download webpagePython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipPython ModulesPython ScopePython PolymorphismPython IteratorsPython InheritancePython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython String OperationsPython Data Types |