0

I am studying Python little by little and one of my main source of studying is somebody else's scripts. I tried to google it, but i can't wrap it up around my head. What means % s.something()%name ?

I can't put exact code in , but for example (sorry little Maya sample):

mc.textScrollList(ams=False,fn='plainLabelFont',p=_ui['mainLay'],sc='%s.listSelected()' % __name__)

def listSelected(): some script which returns list

I read that % it's a module , but i don't understand how it works with definitions and what means 'name' there? I understand question is a little bit bad stated and without any normal example , but if somebody can understand and explain it to me I will really appreciate it !

Thanks!

Vlad
  • 77
  • 6

1 Answers1

1

It is an "older" form of formatting things into strings. The preferred way (python 3 up) is

print('some{:d}'.format(22))  # with format appended and placeholder

or

print ( f'some{22:d}' )  # inline formatting

will format the number 22 into the string as decimal. Yours is similar, but in your example it will format the content of __name__ into your string, __name__ is set by your class/module - resulting in a string of <classname>.listSelected().

See : https://docs.python.org/2/library/string.html#format-specification-mini-language

print('some{:d}'.format(22))
print('Hello %s' % "World")  # similar to yours - using python 3, python 2: use no ()

Output:

some22
Hello World

For more information, see Python string formatting: % vs. .format

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69