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

Python Python Basics Types and Branching String Methods

Pitrov Secondary
Pitrov Secondary
5,121 Points

How 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
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Great 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!!!

Shaun Barbour
Shaun Barbour
Courses Plus Student 1,907 Points

thanks 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
Chris Freeman
Treehouse Moderator 68,423 Points

Shaun Barbour,

len(quote) vs. quote.upper() which would be... (correct?)

  • `len() is a built-in function and may be called directly.
  • In quote.upper(), quote is a str type which has the method upper() 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?

  • The unsatisfying answer is "every is an object". In this case, the object can be a class or built-in type.
  • The first form would be accurately be written as function(parmeters). A function defines its parameters, and is called by supplying arguments
  • The second form you've written would be more accurately written as object.attribute where the attribute may be a defined method 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:

  • Properties are an advanced topic where you define specific functions to be used to get, set, and delete an attribute of a class object.

Post back if you need more help. Good luck!!

I 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)

MDN’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
Pitrov Secondary
5,121 Points

Thank you for your answers. appreciate it!

Thank you, Chris Freeman. I think I finally understnd it now.