Instance variables define differences?

Hello! I wanted to ask what the difference is between defining an instance variable in the __init__() method:

 class MyClass() def __init__(self, **kwargs): self.test1 = 1 self.test2 = 2

and defining an instance variable when creating an instance of the class:

 objekt1 = MyClass(test3 = 3, test4 = 4)

I would be very grateful for answers. But please don't make the explanations too complicated, as I'm still quite new to Python.

LG Code Snake

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

In your second snippet it is only arguments that are handed over to the constructor.

I put it out on an example:

class Person:
  def __init__(self, **kwargs):
    pass

max = Person(name = "Max")
print(max.name) # error

As with you, something is handed over to the constructor. It’s just… Named Argument. A parameter that begins with two asterisks only indicates that several arguments can be handed over (kwargs = keywords arguments, which are also a name (keyword) is assigned. Internally they are packed into a dictionary.

In my design definition, you see that I’m not doing anything in it. That means the argument with the name Max. in the dictionary with the name kwargs packed, but not further exploited. Thus, no attribute (or no instance variable) with the name Name generated and in the last line there is therefore an error case.

When I change the constructor:

class Person:
  def __init__(self, **kwargs):
    self.name = "Hans"

If the errors disappear when the program is executed again, an attribute is created this time. As its starting value “Hans” has been set, you will also get this return in the console.

In order to assign the value of the argument to the attribute instead, the line would have to read as follows:

self.name = kwargs["name"]

In short: Attributes (instance variables) are applied via the object itself. That’ll work out self-Parameter which is transferred to any instance method or constructor or via the object name:

class Person:
  def __init__(self):
    self.name = "Max" # new attribute 'name'

person = Person()
person.age = 77 # new attribute 'age'

If the attribute is created in the constructor, make sure that every object of the class Person also has this attribute. A new Person– Object would be an attribute Name know an attribute age but not.

karotte1386824
1 year ago

The differences between the definition of instance variables in the __init__() method and when creating an instance lie in the fact that the __init__() method is executed when an instance is created. When you define the instance variables in the __init___() method, they are defined each time you create an instance If you create an instance and simultaneously define intostance variables, they are defined only once when the instance is created. In both cases, the instance variables have the same values, but they can be defined in the __init__() method based on arguments received by the constructor, while when creating the instance this is not possible

regex9
1 year ago
Reply to  CodeSnake

I have not understood the sentence

The user who has copied this answer is certainly not.

I would advise you not to deal too much with this text, because it is written cumbersome, implied false facts and thus creates only confusion.

Instance variables are always only valid for one object.

Example:

class Person:
  pass

max = Person()
max.name = "Max"

lisa = Person()
lisa.name = "Lisa"

The two person objects each know an instance variable called Name with its own state. This means that their values are each independent of one another. Changes the attribute value of the object max., does not affect the attribute value of the object of lisa.

You will experience the same behavior if you would define the attributes in a parameterized constructor:

class Person:
  def __init__(self, name):
    self.name = name

max = Person("Max")
lisa = Person("Lisa")

The difference to the top is that the constructor ensures that every instance of Person also an attribute called Name is attached. A parameter is used to variably define the value of each created instance.