What is the best way for a class definition with PEP 526

85 views Asked by At

Lets say I have a class like below:

Class Foo:
    x: int
    y: List[int]
    z: int = 5

    def __init__(self, x:int, z: int = 5):
        self.x = x
        self.y = []
        self.z = z

   ### some other methods

My questions are;

  1. If I am using the types inside the init function, isn't me using types at the class level become redundant?

  2. I want the variable y to be initialized to an empty list for each instance. So, I didn't write the below code as it initialized the y at the class level. Is my understanding correct?

    Class Foo:
       x: int
       y: List[int] = []
       z: int = 5
    
       def __init__(self, x:int, z: int = 5):
           self.x = x
           self.z = z
    

I'm trying to improve my code quality, so if I am missing some primary understanding regarding it, please let me know.

Thank you in advance.

0

There are 0 answers