Electron microscopy
 
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.
          Object -- object is one of instances of the class. which can perform the functionalities which are defined in the class.
          self -- self represents the instance of the class. By using the "self" keyword we can access the attributes and methods of the class in python.
          __init__ -- "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts. This method called when an object is created from the class and it allow the class to initialize the attributes of a class.

          class Car(object):
                def __init__(self, model, color, company, speed_limit):
                    self.color = color
                    self.company = company
                    self.speed_limit = speed_limit
                    self.model = model

          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:

  • Class Definition: The script defines a class named Coordinate. 

    • "self" is used as a placeholder within the class definition to refer to the specific instance of the class being manipulated or accessed. In other words, self serves as a reference to the instance itself. When you create an object of the class, self represents that particular instance. It allows methods within the class to interact with and modify the attributes of the specific instance it's being called on. So, self essentially acts as a pointer to the current instance, enabling seamless manipulation of its attributes and methods.
    • Automatic handling of self: There's no need to explicitly provide an argument for self when calling methods on objects. Python automatically handles passing the instance (self) as the first argument to the method.
    • Understanding self: The use of self within the class definition is a way for Python to identify and manipulate attributes and methods specific to each instance of the class. When a method is called on an object, Python automatically passes the instance (self) as the first parameter to the method. This allows methods to access and modify instance variables, which are attributes unique to each instance.
    • The role of the "self" parameter within class definition is to access the attributes and is a method which is specific to instances of the class.
    • Attribute Access with self: Inside the class definition, when accessing attributes that belong to a specific instance, the self keyword followed by a dot notation is used. This dot notation specifies that the attribute being referred to belongs to the current instance of the class.
    • self Parameter in Methods: In any method defined within the class, including the __init__ method, the first parameter is always self. This parameter acts as a reference to the instance itself, allowing methods to operate on and access attributes of that specific instance.
    • Naming Convention: Although the self parameter can technically be named anything, conventionally it's always named self for clarity and consistency. It's recommended to adhere to this convention to make the code more readable and maintainable.
    • Initialization of Attributes: Inside the __init__ method, which is used for initializing newly created objects, the speaker explains that two assignments are made. The values passed to the method (representing x and y coordinates) are assigned to the respective data attributes (self.x and self.y) of the instance being initialized.
  • __init__ Method: Inside the class, there's a special method called __init__. This method is called the constructor and is automatically invoked when a new instance of the class is created.

    • Parameters x and y are passed to __init__, representing the coordinates of a point. 

    • Instance variables: Data attributes specific to an instance are called instance variables. In the script above, x and y are instance variables of the Coordinate class, representing the coordinates of a point.
    • Inside __init__, the values of x and y are assigned to instance variables self.x and self.y, respectively. These variables hold the coordinates of the point for each instance of the Coordinate class. The parameters x and y in the script represent the coordinates used to create a new Coordinate object, specifying the position of the point in the coordinate system.

    • Initialization of c object: When the line c = Coordinate(6, 7) is executed, it creates a new object of type Coordinate. The parameters (6, 7) are passed to the __init__ method of the Coordinate class.
    • Implicit self parameter: Although the __init__ method of the Coordinate class is defined with three parameters (self, x, and y), when calling Coordinate(6, 7), only two parameters are provided (6 and 7). Python automatically supplies the first parameter (self) when the method is called on an object. In this case, self implicitly refers to the newly created object c. Therefore, effectively, when Coordinate(6, 7) is called, it's equivalent to c.__init__(6, 7).
  • distance Method: Another method named distance is defined within the class. This method calculates the Euclidean distance between two points.
    • It takes another Coordinate object other as a parameter. 

    • It calculates the square of the differences in x and y coordinates between the two points, sums them, and returns the square root of the sum.
  • Object Creation: Two instances of the Coordinate class are created: c and origin, representing points (6, 7) and (0, 0), respectively.
    • self.x vs. x:
      1. When you see self.x, it refers to the attribute x belonging to the instance represented by self.
      2. self.x: This notation refers to the attribute x of the specific instance (self) of the class. It means that the value of x is associated with the particular object instance.
      3. x: Without self, x would be treated as a local variable within the scope of the method where it's used. It could be a parameter passed to the method or a variable defined within the method itself. However, it wouldn't be tied to any specific instance of the class. It's a separate entity from the attribute x associated with the instance.
    • Instance Attribute and Local Variable Distinction:
      1. self.x: This typically represents a piece of data that's unique to each instance of the class. For example, in the Coordinate class, self.x would hold the x-coordinate of a specific point represented by an instance of Coordinate.
      2. x: This could represent any variable within the method's scope. It could be a parameter passed to the method or a temporary variable used for computation within the method. Unlike self.x, it doesn't necessarily have a direct association with a specific instance of the class.
    • Clarification of Instance Attributes:
      1. When you access or modify self.x, you're interacting with the specific x attribute of the object instance. Any changes made to self.x will affect only the x attribute of that particular instance, leaving other instances unaffected.
      2. If you just use x without self, Python will treat it as a local variable within the method's scope, which may or may not have any relationship with the attributes of the class instance.
    • Accessing data attributes: After the Coordinate object c is created, the use of dot notation (c.x, c.y) to access the data attributes (x and y) of the c object. Dot notation is a way to access attributes and methods associated with an object in Python.
    • Understanding Coordinate(6, 7): Coordinate(6, 7) not only creates a new object but also initializes its data attributes (x and y) with the values provided (in this case, 6 and 7). This demonstrates how the __init__ method is automatically called upon object creation to perform initialization tasks.
  • Accessing Attributes: The script then prints the x coordinate of c and origin.
  • Dot notation for accessing data attributes: The same dot notation used to access data attributes within class methods can also be applied to instances of the class outside of the class definition. This means that once an object of a class is created, you can use dot notation to access and manipulate its attributes directly. For example, in the script above, c.x and c.y are used to access the x and y attributes of the c object, respectively. This notation provides a straightforward and consistent way to interact with object attributes throughout your code.
  • Calculating Distance: Finally, it prints the distance between c and origin.
    • Methods behave like functions: Aside from the special self parameter and the dot notation used for attribute access, methods within a class behave just like regular functions. This means that methods can take parameters, perform operations, and return values, similar to functions defined outside of classes. In the script above, the distance method of the Coordinate class is an example of this behavior. It takes the parameter "other", representing another Coordinate object, performs operations to calculate the distance between the two points, and then returns the result. This functionality provides a powerful way to encapsulate related operations within a class and enables code reuse and organization.
    • Usage of self and other parameters: In the distance method definition def distance(self, other), self is a reference to the current instance of the class, while other is another parameter representing a different instance of the class. This method is designed to calculate the distance between two points represented by different instances of the Coordinate class.
    • Calling the distance method: When c.distance(origin) is called, c is an instance of the Coordinate class, and origin is another instance. The dot notation (c.distance(...)) is used to call the distance method on the c object. Python implicitly passes the instance c as the self parameter to the distance method, and origin is passed as the other parameter.

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.

============================================

         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 



















































 

 

 

 

 

=================================================================================