Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ralph Langdon
721 PointsFrom a high level how is the decision made to use a . in front of a command.
From a high level how is the decision made to use a . in front of a command. For instance, an example for the format command (from the lesson) is
print("{} sleeps all night and {} works all day".format (name, name))
However, when I look at the python library link I see that format command documentation from python.org it shows an example that does not use .
format(value, format_spec)
Here is the link from python.org https://docs.python.org/2.6/library/functions.html
Not that concerned about the format command. I am just using that as an example. I just really want to know the fundamental use of .command vs command()
Thanks
6 Answers
Josh Keenan
20,315 PointsA command as you put it with a . is a method.
One without is a function in built to Python or one that you made yourself.
If you are using string.lower() for example, you are using the method .lower() on a string, a method is a function defined within a class (you learn all about it later in the track!). This means only something of that class or a child class can use this method, ie. an integer can't use .lower().
Want to go further down the rabbit hole or is that okay?
Josh Keenan
20,315 PointsNo, a method is different to a function, a method can only be used on something of a certain class, so string.format()
A function is different, these vary depending on whether or not you create it yourself or it is precreated, so print takes a string or variable or integer, pretty much anything. You can create functions yourself that does the same but for strings only, functions are things like del() or print().
Josh Keenan
20,315 PointsNo, a method is different to a function, a method can only be used on something of a certain class, so string.format()
A function is different, these vary depending on whether or not you create it yourself or it is precreated, so print takes a string or variable or integer, pretty much anything. You can create functions yourself that does the same but for strings only, functions are things like del() or print().
Ralph Langdon
721 PointsJosh,
I do remember them mentioning methods now. This is helpful, thanks.
Ralph Langdon
721 PointsActually, I do have one quick question. Can a command be a method and a function in different instances (i.e. format)?
Ralph Langdon
721 PointsOk, thanks again!
Josh Keenan
20,315 PointsHappy to help!