As you found out for yourself, you can use keyword arguments, but you don't even have to do that:
In [4]: print("Hey {0.hey}, let's {0.yo}. Fo' {0.fo}".format(t))
Hey ho, let's go. Fo' sho
In [5]: class Test:
...: hey = 'ho'
...: yo = 'go'
...: fo = 'sho'
...:
In [6]: t = Test()
In [7]: print("Hey {0.hey}, let's {0.yo}. Fo' {0.fo}".format(t))
Hey ho, let's go. Fo' sho
The 0
refers to the zeroth argument to format
, the problem was you had no 1st and second argument, because you didn't need it.
Warning as an aside:
Also, since you seem to be coming to python from other languages, you might be making a common mistake. Note that the way you have defined your class;
class Test:
hey = 'ho'
yo = 'go'
fo = 'sho'
uses only class-level variables, which will act like static members to borrow terminology from other languages. In other words, hey
, yo
, and fo
are not instance attributes, although your instances have access to the class-level namespace. Check out this answer. Of course, this doesn't matter for the purposes of this question, but it can lead to bugs if you don't understand the semantics of the class definition.