Home > Mobile >  Understanding self in python
Understanding self in python

Time:10-17

I am trying to understand self in python, but not getting it correctly. For example consider the below code:

class relsel_environment:
    def __init__(self, seed):
        self.seed(seed=seed) 

        self.step_count = 0 
        
        self.state = 0 

I understood that first statement is the class , second is the method, in 4th statement step_count is set to zero and in last state is set to zero.

But I am not getting what self.seed(seed = seed) is doing. Any help in this regard is highly appreciated.

CodePudding user response:

When you do self.seed(seed=seed), you are telling to the current relsell_environment instance that, execute the method seed and using the argument seed (that use that method) with the value of the variable seed received in the __init__ function.

In other words, when you initialize a instance of the class, the first thing you do in the constructor is call the method seed() and pass the argument with the same name received in the constructor.

You need to know that you class don't have the method seed(), so it will raise an error.

  • Related