avoid the use of globals #4

Closed
opened 2018-09-30 01:16:15 +00:00 by coolaj86 · 3 comments

Right now you have a lot of global variables. A generally accepted better way to program (and part of The Zen of Python) is to use parameters

so instead of

def encrypt():

you would have

def encrypt(str, shift):
Right now you have a lot of global variables. A generally accepted better way to program (and part of [The Zen of Python](https://www.python.org/dev/peps/pep-0020/)) is to use parameters so instead of ```py def encrypt(): ``` you would have ```py def encrypt(str, shift): ```
Owner

Macej was saying that it was better not to use global variables. I just hadn't figured out how to do it better yet.

Macej was saying that it was better not to use global variables. I just hadn't figured out how to do it better yet.
Author

The technical term "variables" refers to free-standing references to values.

x = 1

The technical term "arguments" refers to references to values which are "received" by a function

def foo(x, y)
  x = 1

The technical term "parameters" refers to the definition of "arguments" in the function definition itself

def foo(x, y)

When you read the command line values into a local variable you can then pass it into the function as an argument.

cmd = sys.argv[1]
msg = sys.argv[2]
shift = sys.argv[3]

if "enable" == cmd:
  encrypt(msg, shift)

(not sure if that's real python, but I think it looks something like that)

The technical term "variables" refers to free-standing references to values. ```py x = 1 ``` The technical term "arguments" refers to references to values which are "received" by a function ```py def foo(x, y) x = 1 ``` The technical term "parameters" refers to the definition of "arguments" in the function definition itself ```py def foo(x, y) ``` When you read the command line values into a local variable you can then pass it into the function as an argument. ```py cmd = sys.argv[1] msg = sys.argv[2] shift = sys.argv[3] if "enable" == cmd: encrypt(msg, shift) ``` (not sure if that's real python, but I think it looks something like that)
Owner

Thank you. Fixed in 024c033ac2

Thank you. Fixed in https://git.coolaj86.com/josh/python-hard-way/commit/024c033ac2a56097c7b2dbddbd75a233100da96f
josh closed this issue 2018-10-06 17:12:14 +00:00
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: josh/python-hard-way#4
No description provided.