Built in function syntax


#1

I would love an explanation to the syntax of the built in functions in Python (e.g. {.format()}. I’m not fully understanding what goes before the period and what goes in the parenthesis (generally, not only for {.format()}. I understand that it’s the arguments that goes in the parenthesis, and in a function I’m writing that makes sense (any info I want to send into the function), but I’m not sure I understand in the built in functions what’s the difference between what you put in front of the point and what you use as arguments. Can all bult in functions have something before it with a period first?


#2

You just put a string before:

test.format()

And test can be anything that’s a format. One way to think about .format() is:

“Get test’s format”

. is the get operation, so you are getting the test string’s format. THEN, you call it. Beginners frequently think .format() is one thing (call format) but it’s really 2 steps. Get tests’s format, THEN call it.


#3

Thanks Zed, that makes sense.

So more generally, how do I know what functions I can put a . in front of? Can I add a string in front of any function?

I want to make sure I fully understand the difference of what to use as arguments, and what to call before the function. Is it right that I want to get what I want to modify (in this case a string) before I call the function, and then I use the arguments to modify the string?

Thanks again for helping out!


#4

Zed did a really good OOP (Object Oriented Programming) series on this that was enlightening.

The dot, period, ‘.’ is a shortcut syntax/notation to get the operation (as he explains above). Then you call it with parenthesis.

If you use an editor like vscode, it’s interesting to try the dot after things to see what the intelli-sence offers you. This will be elegable functions (actions) that can be performed.

So its not a case of what functions can you put a dot in front of, it’s the other way around, what things (classes) might have associated actions/functions. So if you had a list for example, lists have a load of functions that perform actions, like .append() so you can add things to the list.

When you get to creating your own classes and functions, it makes more sense. You can have:

my_thing.does_this_action()

#5

Thank you so much, I think I need to keep programming for this to sink in.


#6

I tried finding that OOP series but can’t find it. Any chance you would link it for me?


#7

https://shop.learncodethehardway.org/products/video/973/lclive-oopschool-s1/?query=&page=3

You may need to be a ‘Live’ subscriber to access, which if you are not, I strongly recommend signing up. In terms of value for money and hands on support, it’s second to none!


#8

Thank you so much. I’ve thought some more about this and I’m still not sure I’m onboard…

I’ll give you an example: When I open a document I put the document name as an argument, but when I close a document I put the document name in front of the get operation, how does this make sense?

file = open("file.txt")
file.close()

#9

It’s ok, sooner or later things will ‘click’ but we’ll keep at it until it does.

In your example it helps to say the sequence of things requested out loud.

file = open(“file.txt”)

So you are asking Python to use its ‘open’ command on a file that you’ve specified in quotes. You want to assign the contents of that file (that you’ve opened) to a variable you are calling ‘file’. This is important as without the assignment you have just opened a file and cannot do anything with it.

To help where I think you are getting confused, let’s use something clearer for the variable:

the_stuff_from_the_file = open(“file.txt”)

Now you’ve captured the contents, you might do some stuff with with like:

Make it uppercase, so you call that function on the contents

the_stuff_from_the_file.upper()

Maybe split it, by calling the split function and telling it to look for whitespace as the point to split (this is the argument in the parentheses)

the_stuff_from_the_file.split(‘ ‘)

And then print it, which is different as you need the contents to be an argument.

print(the_stuff_from_the_file)

What happens if you do this?

the_stuff_from_the_file.print() 

So in the close example…

file.close()

…you are calling a close function on variable that holds the file contents.

the_stuff_from_the_file.close()

It’s worth noting that “file.txt” is the name of the file you are opening. file is an assignment, not the file of the type txt.

This is confusing when you start out, and highlights that using descriptive names for things, or good commenting is really important, otherwise…

 class Stuff():                                                                           
                                                                           
    def print_stuff(self, my_stuff):                                            
        print(my_stuff)                                                         
                                                                                
more_stuff = Stuff()                                                            
more_stuff.print_stuff("stuff")  

#10

Actually @Malin, where are you in the book? It may be time for you to just write this question down until you get to Exercise 42 or 43. Then you cover OOP.


#11

I have write an answer to a similar question to ex07. Maybe it helps. You can find it here: Study drill 6 and 7 exercise 15
See section about Study Drill 7.


#12

Thank you so much, your explanation that open() is a function and .close() is a object method helped me understand that they are different things.


#13

Thank you so much for taking the time to explain this with all those examples. It helped me see the difference between how they are used.

When I wrote str.print() Python said 'str' object has no attribute 'print'. . I’ll keep working. I’ll keep working for now. As @zedshaw said, I haven’t gotten to exercise 42 yet.


#14

Yeah I got a bit carried away there. Of course @zedshaw advice is best. You’ll hit lots of stages like this when it seems make no sense. Write it down, and come back to it. Its a really good measure of progress.

something.print() will error. But you could argue why? You are calling the function on the object. But its doesn’t work that way, somethings in programming ‘just are’. But Zed always explains why historically, which is super cool.