Python Forum
Are my descriptions (comments) correct here? [newbie needs help]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are my descriptions (comments) correct here? [newbie needs help]
#1
I'm curious what the name of the "slots" are that I mentioned in my comments. These are slots in the function. There are two in the parentheses in this function.

This is from an exercise in learn python the hard way.

#This line defines the function cheese_and_crackers and it assigns two "slots" which have 
#distinct names that can be used by other pieces of code to correspond to the two variables
#in the strings in this function.
def cheese_and_crackers(cheese_count, boxes_of_crackers):
#This line is the first string printed by this function and it takes the first variable in any
#piece of code that calls this function.
	print "You have %d cheeses!" % cheese_count
#This line is a string which takes input from a variable at the part where it says "%d"
	print "You have %d boxes of crackers!" % boxes_of_crackers
#This line is another string from the function cheese_and_crackers.
	print "Man that's enough for a party!"
#This line is a string which is the last printed element of the function cheese_and_crackers.
	print "get a blanket.\n"
	
#This prints a line above the standard output from the function cheese_and_crackers.
print "We can just give the function numbers directly:"
#This line calls the function and inputs 20 and 30 into the respective "slots" (two of them)
#in this function. Maybe these are called arguments?
cheese_and_crackers(20, 30)

#This line prints on top of the output from the function cheese_and_crackers when it takes the
#input from "amount_of_cheese" and "amount_of_crackers"
print "OR, we can use variables from our script:"
#This line defines the variable "amount_of_cheese" as 10
amount_of_cheese = 10
#this line defines the variable amount_of_crackers as 50
amount_of_crackers = 50
#This line takes the variables amount_of_cheese and amount_of_crackers and plugs them into the 
#function cheese_and_crackers defined at the start
cheese_and_crackers(amount_of_cheese,amount_of_crackers)

#This line prints a "new" statement above the output of cheese_and_crackers function output
print "We can even do math inside too:"
#This line calls the function cheese and crackers from the top and it inserts the value of the
#combined numbers and uses each as a variable as seen in the defined function's 2 variable set up
#Then each value is printed in the designated spot in the function cheese_and_crackers
cheese_and_crackers(10 + 20, 5 + 6)

#This line prints a "new" statement above the output of cheese_and_crackers function output
print "And we can combine the two, variables and math:"
#This line calls the function cheese and crackers from the top and it inserts the value of the
#variables in the parentheses and combines them with the numbers seen next to them and it plugs
#the result into the strings (and variables) stipulated in the function cheese_and_crackers
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
Reply
#2
When in the function definition, these 'slots' are called parameters, e.g.
def cheese_and_crackers(cheese_count, boxes_of_crackers)
function cheese_and_crackers has two parameters - cheese_count and boxes_of_crackers
later on, in your code, when you call the function, the actual values that you pass to the function call are called arguments. In this
cheese_and_crackers(20, 30)
20 and 30 are arguments. There are positional and named or keyword arguments.
Please refer to python glossary for in-depth definition of arguments and parameters
Also, check the FAQ on What is the difference between arguments and parameters?

All that said - couple of things regarding your code. Your examples are perfectly valid code and many people would do it the same way. all of your examples are using positional arguments. It's more clear and easy to follow the code if you get the habit to pass named/keyword arguments. e.g.
cheese_and_crackers(cheese_count=20, boxes_of_cracker=30)
or
cheese_and_crackers(cheese_count=amount_of_cheese,boxes_of_cracker=amount_of_crackers)
you can mix positional and named arguments, however positional arguments ALWAYS come before the named arguments. i.e. you cannot have positional arguments after you have supplied named arguments.

also you can have default values, e.g
def cheese_and_crackers(cheese_count=20, boxes_of_crackers=30)
and then call the function just like
cheese_and_crackers()
and it will use the default values. Of course you can pass one argument and use the default value for the other, etc.

Finally - you are using print statements and old style string formatting in your print statements. So you are using python2 and as a newbie, starting now to learn python you should start learning python3 (latest one in python 3.6). In python3 print is a function, not statement.
I assume you study python2 because of the book, but I would recommend to find a book that is based on python3. There is even LPTHW version for python3, but Learn python the hard way is really controversial textbook, and one of the reasons is exactly because of using such terms as 'slots', while there are accepted terms in the programming.

As to the string formatting, instead of
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
it is better to use

print("You have {} cheeses!".format(cheese_count))
print("You have {} boxes of crackers!".format(boxes_of_crackers))
or in python3.6 you can use the new formatted strings
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers!")
to read more on string formatting check this
https://docs.python.org/3.6/library/stri...ing-syntax
Reply
#3
(Oct-10-2017, 06:19 AM)buran Wrote:
def cheese_and_crackers(cheese_count=20, boxes_of_crackers=30)
and then call the function just like
cheese_and_crackers()
and it will use the default values. Of course you can pass one argument and use the default value for the other, etc.

When you say you can "pass one argument" do you mean that you can input a different value than the default? IOW, taking your example, you could write

cheese_and_crackers(cheese_count=50)
assuming
def cheese_and_crackers(cheese_count=20, boxes_of_crackers=30)
And then it would use the new value for "cheese_count" and the default value for "boxes_of_crackers"?

Thanks for the help :)
Reply
#4
(Oct-15-2017, 06:01 AM)MattSS102 Wrote: And then it would use the new value for "cheese_count" and the default value for "boxes_of_crackers"?

Yes, that is correct. But you could have found for yourself by simply running the code.
Reply
#5
Documentation is great, but you can over do it, in which case many will not try to read it at all.
using a triple quote immediately after the method or function definition line is sufficient, like:
    def get_files(self):
        """
        Get all zip files fron filelist, and extract on the fly
        :return: None
        """
This style will show up nicely when you use help('class_name') as well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 497 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,790 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,177 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,302 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Multiline comments macfanpl 6 2,729 May-07-2020, 08:14 PM
Last Post: macfanpl
  Can the comments produce errors in python? newbieAuggie2019 9 4,261 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Variable comments on Visual Studio Code sal 2 3,183 Oct-19-2019, 02:13 PM
Last Post: sal
  Regular expression to fetch comments in C using Python pikkip 4 4,890 Jan-24-2017, 09:21 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020