Question

How could I call a self.value in a definition of a function?

class toto :
    def __init__(self):
         self.titi = "titi"
    def printiti(self,titi=self.titi):
          print(titi)
Was it helpful?

Solution

This is how it is done:

  def printiti(self, titi=None):
    if titi is None:
      titi = self.titi
    print titi

This is a common python idiom (setting default value of argument to None and checking it in method's body).

OTHER TIPS

class Toto:
    def __init__(self):
         self.titi = "titi"

    def printiti(self, titi=None):
         if titi is None:
             titi = self.titi
         print(titi)

Class names are generally Upper Case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top