views:

54

answers:

2

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)
+4  A: 

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).

gruszczy
Thanks for the help Regards Bussiere
it does not work on py3k :-)
joaquin
Guess what, print is a function in 3k :) Instead write: `print(titi)`
gruszczy
+2  A: 
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.

S.Lott
Thanks for the help Bussiere
+1 for following pep8
Daenyth
@Daenyth: I hate parts of PEP8. Compliance is entirely accidental because of copy and paste from the question. Don't thank me. Thank user462794.
S.Lott