self and __init__ method in Class - Python for Integrated Circuits - - An Online Book - |
||||||||
Python for Integrated Circuits http://www.globalsino.com/ICs/ | ||||||||
Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix | ||||||||
================================================================================= Class --
Class is a set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.
In technical terms we can say that class is a blue print for individual objects with exact behaviour. class Car(object): maruthi_suzuki = Car("Toyota", "black", "Corola", 60) One example of self and __init__ method in class is (code): Output: Here, it defines a method with a specific name and purpose that is recognized by Python's interpreter. In this case, __init__ is one of Python's special methods used for initializing newly created objects:
By implementing the special method __init__, Python knows that whenever a new object of type Coordinate is created, it should call this method to initialize the object with the provided x and y coordinates. The self parameter represents the instance itself, while x and y are the coordinates to be assigned to that instance. Additionally, the distance method calculates the Euclidean distance between two points, taking another Coordinate object as a parameter. In the script, we create two instances of Coordinate, access their attributes, and calculate the distance between them. The parameters x and y as representing how you create a coordinate object, they're referring to the fact that these parameters are used to specify the x and y coordinates of a point when creating a new instance of the Coordinate class. "self" is used within a class definition to refer to the instance of the class itself. When defining methods within a class, including the __init__ method, the first parameter conventionally named self is used to represent the instance of the class being manipulated or accessed. This parameter acts as a reference to the particular instance of the class, allowing methods to interact with and modify the attributes of that specific instance. ============================================
|
||||||||
================================================================================= | ||||||||
|
||||||||