Search code examples
pythonoopconventionsconvention

Python - Accessing instance attribute without `self.` inside __init__


My question is largely one of style or convention; however, it could have consequences which are not obvious to me that would effect which style is used.

If I have a class where instance attributes are defined by parameters to __init__, is it acceptable to access these parameters directly, rather than by the instance attribute? An example is given below:

from some_module import MyOtherClass

class MyClass(object)
    def __init__(self, uno, dos):
        self.uno = uno
        self.dos = dos

        """ This? """
        MyOtherClass(uno)
        MyOtherClass(dos)

        """ Or this? """
        MyOtherClass(self.uno)
        MyOtherClass(self.dos)

Solution

  • Sometimes there is extra logic in each initialization:

    def __init__(self, uno, dos=None):
        self.uno = list(uno)
        self.dos = dos or default_dos()
    

    In that case, reusing the argument would result in an error.