Learn Python 3, The Hard Way, Ex. 23

The author is right! THIS IS HARD.

@bestever, you understood my questions before, so hopefully, you will follow these questions too!

My question is regarding lines 16 and 17. As command-line arguments, we pass utf8 and strict. Why do we not pass those same arguments to the encode and decode functions? The author has the following:

raw_bytes = next_lang.encode(encoding, errors=errors)

I would expect to see:

raw_bytes = next_lang.encode(input_encoding, errors=error) #utf8 = input_encoding strict = error

As both versions work, why does Mr. Shaw choose to it how he did? Is one way preferred over the other, or should I write the code the way that it makes sense to me?

CleanShot 2020-08-23 at 11.33.53@2x

I see what you’re saying. I answered your email but this is what I was saying when I said post the code here.

So, at the top I do this:

script, input_encoding, error = sys.argv

That “explodes” your command line arguments into the script level variables:

script
input_encoding
error

These variables are now available to READ in the whole script everywhere. You don’t have to pass them to the functions to use them. In the lines where I do this:

errors=error

I am setting a key=value style of argument to that function. These work like the positional arguments in my last post (add(a,b)) but they are given a name. So, this is like giving an optional setting to the function for how it’s supposed to handle errors. Go check out the docs to see what is available:

https://docs.python.org/3/howto/unicode.html

Mr. Shaw,

I understand errors = error, but the PDF version of the book, as well as the video, has errors = errors. That is the source of my confusion. In my screen capture, lines 17 and 19 that contain errors = error are my attempt at playing with the code and trying to understand it before posting to the forum. My apologies for the confusion.

So, should the code read errors = errors (plural) or errors = error (singular)? If it is the latter, I understand.

@zedshaw Appreciate your time!

am gonna answer what i understand from the ex23
look remember in last time when we said that when we define a function , the arguments we enter are just place holders for the real arguments (aka parameters ,place holders) ok you don’t then look at this ex:

#am not indenting in the example

def talking(Morty, Roky):
print (’%s talking to %s’ % (Morty,Roky) )

#know am gonna call the function(aka run it , use it)
talking(me, him)
#this will print : me talking to him
#calling it again with deffernt parameters(aka arguments ,informaly place holders)
tlking(Roky, Morty)
#this will print : Roky talking to Morty

Notice : that the parameters in the function defintion only hold the places or define the arguments in the code only and has nothing to do with calling it .

Now lets tallk about the ex23:

start reading from the line before the end there we define languges , then we give it to main function as the first argument and we place 2 new place holders in the second and 3er argument of main . main is defined to take the second and third argument from argv so it will do . now when we call the main function at the last line of the code it will start runing from the first line after its definiton and it will not use the place holders of its defintion but it will use the new place holders(aka parameters,arguments) we give it when we call it at the end of the code so it uses them now not the the once in the definiton. that is why we use encoding .
about the …=sys.argv is just like saying from sys that you impoted get argv and set it to those variables in the lefet of the equal (=) . and it works like that and you can use it no since about it just remember it :slight_smile: it is mentiond in the 3 editon that it cantake up to 5 arguments if i still remeber it right.

about the errors = errors and encoding = utf-8 , i don’t know , but try to take one errors and = and see what will happen and take the encoding and leave the utf-8 and chk what happens. if not understood then check the open function usage in python 3 ,or jow to use function(argument , argument2=argument3) in paython 3
put any funtcion and arguments instaid of there names i have writed and check them online . and let me know about them here please as i have never incounter them in python2

if you still have any dbouts about what i wrote above please aske more i will then explain more untill i make my ideas clear to be understood .

good luck bro :slight_smile:

@bestever. I appreciate your response. Thank you! @zedshaw provided a detailed explanation as well, so I have a much better understanding than when I generated my original post. Now, if I could just figure this 3rd study drill out, I will be cooking with gas! @zedshaw expected this to take a week, and he sure wasn’t lying.

I may move on and come back to this when I have better knowledge. However, I don’t want to go far without understanding this exercise completely.

Again, thank you for the assist.

sometimes you just need to ignore some parts of the exercise and but after reading it and try to understand it , and then pass to the next exercise . after a few exercises if you understand them well just continue to finish the book but if you find some problem that you couldn’t solve and you remember that the book solve it with a way that you did not understand and you passed it ,then comeback to it and once you will ready it again you will understand it this time (understanding something is just to know when and how to use this thing and nothing more )

Definitely don’t think you have to do every exercise completey before continuing on. It’s more that you do what you can, move on and do a few more exercises, and then previous study drills make more sense.