Consider this code:
class Foo:
def geta(self):
self.a = 'lie'
return 'this is {self.a}'.format(?)
What should I write instead of the question mark so that the string will be formatted correctly?
What you probably are looking for is
'this is {0.a}'.format(self)
'this is {.a}'.format(self)
'this is {o.a}'.format(o=self)
'this is {self.a}'.format(self=self)
Note, however, that you are missing at least a method in your class.
Directly under the class scope there is no such thing as self
.
The reference you include inside the brackets refers to either a number indicating the index of the argument passed to format, or a name directing to a named argument in the format call. Like that:
class Foo:
def geta(self):
self.a = 'lie'
return 'this is {self.a}'.format(self=self)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With