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]
#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


Messages In This Thread
RE: Are my descriptions (comments) correct here? [newbie needs help] - by buran - Oct-10-2017, 06:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 670 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,845 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,227 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,357 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Multiline comments macfanpl 6 2,819 May-07-2020, 08:14 PM
Last Post: macfanpl
  Can the comments produce errors in python? newbieAuggie2019 9 4,408 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Variable comments on Visual Studio Code sal 2 3,269 Oct-19-2019, 02:13 PM
Last Post: sal
  Regular expression to fetch comments in C using Python pikkip 4 4,954 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