You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A class is a user-defined blueprint or prototype from which object are created.
Classes provide a means of bundling data and functionality together.
Creating a new class creates a new type of object, allowing new instances of that type to be made.
Class Definition:
class ClassName:
# Statement
Object
An Object is an instance of a Class.
A class is like a blueprint while an instance is a copy of the class withActual Values.
An object consists ofState (represented by the attributes of an object, also reflects the properties of an object),Behavior (represented by the methods of an object, also reflects the response of an object to other object), andIdentity (gives an unique name to an object and enables one object to interact with other objects)
Object Definition:
obj = ClassName()
print(obj.attr)
4.1.2 initialization method 初始化方法:
__init__(self [, args, ...])
4.1.3 Object Property and Method 对象属性与方法
4.2 Private Property and Private Method
私有属性和私有方法
3 types of Class resources
Public
All members in a Python class are public by default
Protected
Protected members of a class are accessible from within the class and are also available to its sub-classes
No other environment is permitted access to it.
This enables specific resources of the parent class to be inherited by the child class.
Python's convention to make an instance variable protected is to add a prefix _ (single underscore) to it.
Private
Private members of the class are denied access from the environment outside the class.
Private Property to hide Property: __(propertyName)
Private Method: __(methodName)
isinstance() built-in function
4.3 Magic(Dunder) Method 魔法方法
Dunder or Magic methods in Python are the methods having two prefixs and suffix underscores in the method name.
Dunder: means "Double Under (Underscores)"
Use "dir(int)" to list the magic functions in Python