-2

List item

class Car:  
    def __init__(self, color, brand, number_of_seats):  
        self.color = color  
        self.brand = brand  
        self.number_of_seats = number_of_seats  
        self.number_of_wheels = 4  
        self.registration_number = GenerateRegistrationNumber()

Hi all,
1)Referring to the above example, could anyone tell me what is the difference between specific attributed and "the other" attributes? What will happen if registration_number is treated as a specific attribute?

2)

class MyInteger:  
    def __init__(self, newvalue):    
        # imagine self as an index card.  
        # under the heading of "value", we will write  
        # the contents of the variable newvalue.  
        self.value = newvalue  

If we consider this example, shouldn't it be self.newvalue = newvalue?

  • @Amadan could you help me with this? I seek to you because your explanation to "Why do we use __init__ in Python classes?" has been the best I have come across so far. – Pritam Changkakoty Mar 28 '21 at 04:33
  • 1
    It is not clear. What do you mean by "*specific attributed and "the other" attributes*" and "*if registration_number is treated as a specific attribute*"? You can name the instance attributes to anything (ex. `self.color`, `self._color`, `self.colour`, `self.colorrrrrrr`, etc.) and the parameters to `__init__` can also be named to anything (`new_value`, `newvalue`, `newval`). It should work as long as you reference them correctly. It just makes it more readable to name them to be consistently the same. – Gino Mempin Mar 28 '21 at 04:38
  • @GinoMempin thanks for the reply. i was referring to this article "https://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes/8609238#8609238" – Pritam Changkakoty Mar 28 '21 at 04:41
  • here it was mentioned"This way, we are creating an instance of the Car class. The __init__ is the one that is handling our specific attributes (like color or brand) and its generating the other attributes, like registration_number." hence the terms – Pritam Changkakoty Mar 28 '21 at 04:42
  • so it is fine to use self.newvalue = newvalue instead of self.value = newvalue ? – Pritam Changkakoty Mar 28 '21 at 04:44

2 Answers2

1
  1. I think I know what you're asking (let me know if I'm wrong), but I think you're asking what the difference is between the attributes that are assigned by the parameters of __init__ (Instance Attributes), ones that are assigned inside the __init__ method but not with parameters (also Instance Attributes), and ones that are not assigned in the initialiser at all (Class Attributes). The difference here is that all (well, pretty much all) cars have 4 wheels, and the number plate is generated, not supplied. You could also do this, for example:

    class Car:
        number_of_wheels = 4
    
        def __init__(self, color, brand, number_of_seats):  
            self.color = color  
            self.brand = brand  
            self.number_of_seats = number_of_seats
            self.registration_number = GenerateRegistrationNumber()
    

    As the number of wheels here is always assigned to the same value, across all instances, it is said to be a "Class Attribute" in this case. All other attributes here are “Instance Attributes” as they are specifically assigned to each instance. For a slightly better explanation, I recommend reading this: https://www.geeksforgeeks.org/class-instance-attributes-python/

  2. It doesn't actually matter what the instance attribute (self.value here) is called, you could call it whatever you want and it'd still work, but in most cases, you would indeed want to name the attribute the same as the parameter.

CallumLRT
  • 71
  • 4
  • thanks for the response. You are spot on! the only doubt I have is between the instant attributes (assigned inside but not with parameters AND assigned along with parameters). – Pritam Changkakoty Mar 28 '21 at 12:00
  • So just imagine the instance attributes as ones that are assigned to the object (also called an “instance” of the class) individually, so they’ll always be assigned as part of each object (even if their values are the same every time). As for class attributes, they won’t be specific to each instance and will remain the same for any instance of the class, regardless of how the initialiser instantiates the class. – CallumLRT Mar 28 '21 at 13:20
0

init function also called as magic function is a constructor function for a class. if you remember in java whenever we make a class the constructor method should have the same name as the classname but this is not the scenario in python . In python we make use of the init method

the difference between the class attributes and instance attributes is that the class attributes are common to every object that is created but the instance attributes are only accessible by the object that is created.

consider a example where data of students in a class is stored. In such case the class division will be same for all the students of that particular class so it can be common but names of all students are different and also their marks and so on and hence they should be different for everyone

in previous scenario the class division can be class attribute and the data of student like name , marks has to be instance attributes

examples of class attribute is as shown

class A:

Division = 5A

here the division is a class attribute

class B:

  def __init__(self,name,marks):
      self.name = name
      self.marks = marks

here the name and marks are instance variables now here we can also write self.username = name because we are storing the value of name variable in self.username so you can write any name there is no constraint on that

Also whenever you write __ in front of method or variable it means that the attribute is private and accessible by only class.