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 trialPitrov Secondary
5,121 PointsHow do I know when to quote.something() or something(quote)?
Now I learned that I can for example quote.upper() or like help(str) why not str.help()? And does it always have to have () at the end something.something() or can I do sometimes something.something without ()? Is there a way I can know if I need to do it with a . or I have to write it as an agrument?
5 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsGreat question!
The form function(argument) is used for built-in functions (those defined as a core part of Python) and for functions defined in the user’s program (like those you create for the coding challenges). help(object)
is a built-in function. first_and_last_four(argument)
is a function you’ll define for a challenge.
The form object.method() is used to execute functions defined as part of a class or object. These functions are called “methods”. str.help()
would not work because the string object does not have a “help” method defined. str.lower()
is a string method.
Using parens () at the end, with or sometimes without arguments, signifies you are calling a function or method to be executed.
Advanced concept: without the parens, you would be referring to the function or method itself, which is allowed. This is used when you wish to pass a function as an argument but not executed it immediately.
Post back if you need more help. Good luck!!!
f4u57
6,743 PointsI will try my best to explain, quote must likely a str, .upper() is a method for str data type, you can see method as tools for a certain data type or function. Now help() is a build-in python function, but if you do str.help() then i.help() become a method which does not exist inside the str data type. So help() have be always follow this format help(str), because it a function not a method for a data type. To call a function you have to follow this format function_name(whatever). To know when you nee the . you have to decied wether not you are trying to call a medthod. When I say "date type" I am refering to (str, int, bolean, float)
mryoung
4,156 PointsMDN’s website is good for learning syntax of methods. So, if you’re trying to do object.method() and just want to make sure you’re on the right track...you just look it up and it’ll show it, or maybe you’ll need to pass arguments to it, etc.
Pitrov Secondary
5,121 PointsThank you for your answers. appreciate it!
Remco Hekker
3,356 PointsThank you, Chris Freeman. I think I finally understnd it now.
Shaun Barbour
Courses Plus Student 1,907 PointsShaun Barbour
Courses Plus Student 1,907 Pointsthanks for your thoughtful answer. however, i'm still a little confused. my issue is probably just semantics.
using the video...
len(quote) vs. quote.upper()
which would be... (correct?)
function(object) vs. object.function()
am i using "function" and "object" correctly?
in the teacher's notes, he uses: 1) attributes, 2) properties, 3) methods, and 4) behaviors. then he throws around the word: 5) function
i think i understand what an object is. can you please define these other five terms? are they all five different distinct terms with specific definitions? are some of them just synonyms for something?
it would help me if i understood the terms objectively. i think when he uses words interchangeably, it confuses me.
thanks so much! by far my favorite teacher! good stuff.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsShaun Barbour,
len(quote) vs. quote.upper() which would be... (correct?)
quote.upper()
,quote
is astr
type which has the methodupper()
available. A method is a function defined with a class or built-in type (str
,int
,float
, etc)function(object) vs. object.function() am i using "function" and "object" correctly?
object
can be a class or built-in type.function(parmeters)
. A function defines its parameters, and is called by supplying argumentsobject.attribute
where theattribute
may be a definedmethod
or another object.in the teacher's notes, he uses: 1) attributes, 2) properties, 3) methods, and 4) behaviors. then he throws around the word: 5) function
I didn't see where the teacher used the terms "properties" of "behaviors". He may have been using those terms in the generic English sense and not as a Python term. That said:
Post back if you need more help. Good luck!!
Shaun Barbour
Courses Plus Student 1,907 PointsShaun Barbour
Courses Plus Student 1,907 Pointsbrilliant! thanks so much! very helpful.