gpt4 book ai didi

python - 不确定我的 python 解释性评论

转载 作者:行者123 更新时间:2023-12-01 04:42:16 24 4
gpt4 key购买 nike

我正在做“艰难学习 Python”练习 15

学习练习是用注释来解释每一行,但我对我的解释感到不确定。我正在寻找对我的错误的纠正和改进。正确的术语使用是我最担心的,特别是当涉及到变量和文件对象时。

这是纯代码

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

这是我的解释性注释不充分的代码。

#the following is what is run from commandline
#each word(?) following python is an argument
#python ex15.py ex15_example.txt


#imports the "argument variable" module from the sys package
from sys import argv

#assigns commandline arguments to variables
script, filename = argv

#creates "txt" variable that creates a file-object of the filename variable
txt = open(filename)

#prints written string followed by filename variable
print "Here's your file %r:" % filename
#reads and prints variable "txt" which is a file object
print txt.read()

#prints string asking for raw_input
print "Type the filename again:"
#prompts you for raw_input and takes your raw_input and makes file_again variable
file_again = raw_input ("> ")

#creates txt_again file-object of file_again variable
txt_again = open(file_again)

#reads and prints txt_again file-object
print txt_again.read()

最佳答案

这些注释的目的只是为了让您编写它们,并通过这样做来学习和记住每一行的作用。他们不必遵循正确注释的规则(不像任何人在实际代码中注释每一行,那么使用 python 做什么?哈哈)

所以,既然这样,让我像你一样浏览每一行。

#the following is what is run from commandline
#each word(?) following python is an argument
#python ex15.py ex15_example.txt
# (Correct. )

#imports the "argument variable" module from the sys package
# (It doesn't work like that. sys is a module,
# meaning a sis.py file stored somewhere else is accessed and interpreted.
# And argv is an object declared within
# said file (could be a class, a global variable, etc.
# for example. in this case, it's the list of command parameters. )
from sys import argv

#assigns commandline arguments to variables
# (Yeah, since he's doing this, it means argv has 2 items inside, and
# each is assigned to the respective variable).
script, filename = argv

#creates "txt" variable that creates a file-object of the filename variable
#(Yup)
txt = open(filename)

#prints written string followed by filename variable
#(This is a formatted string, similar to how you use printf in C.
# If you don't know them now, you will later, probably.)
print "Here's your file %r:" % filename
#reads and prints variable "txt" which is a file object
#(the method 'read' basically goes through the remaining unread
#lines and returns them as a big-ass string, which is then printed
# by you)
print txt.read()

#prints string asking for raw_input
print "Type the filename again:"
#prompts you for raw_input and takes your raw_input and makes file_again variable
# (yup, nothing to see here...)
file_again = raw_input ("> ")

#creates txt_again file-object of file_again variable
# (yup)
txt_again = open(file_again)

#reads and prints txt_again file-object
# (no big surprises here. BTW, if you use txt.read() again, you'll get
# an empty string. If you already know iterators from some other language,
# think of file objects as iterators of a file. Once they are used once,
# you need another one (you can reset this one, though, with some other method).
print txt_again.read()

关于python - 不确定我的 python 解释性评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30339890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com