base64.b64encode(data)
Hi to everyone. Let's say I have some binary data, be it whatever, in the 'data' variable. After calling the following line b64_encoded_data = base64.b64encode(data) my b64_encoded_data variables holds, would you believe it, a string as bytes!. That is, the b64_encoded_data variable is of type 'bytes' and when you peek inside it's a string (made up of what seems to be only characters that exist in Base 64). Why isn't it a string yet? In fact, I now on that variable have to apply the decode('utf-8') method to get a string object holding the exact same sequence of characters as was held by b64_encoded_data bytes variable. I'm a little confused as to why I would even have to apply the .decode('utf-8') method - why doesn't base64.b64encode provide us with a result that is a 'str'? -- https://mail.python.org/mailman/listinfo/python-list
Re: the global keyword:
Much thanks to all for their time, but Ned in particular...I learned something new about Python!! On Saturday, 11 June 2016 22:48:32 UTC-5, Ned Batchelder wrote: > On Saturday, June 11, 2016 at 11:38:33 PM UTC-4, Steven D'Aprano wrote: > > On Sun, 12 Jun 2016 11:26 am, Random832 wrote: > > > > > On Sat, Jun 11, 2016, at 20:09, MRAB wrote: > > >> Not true. Importing doesn't copy the value. > > >> > > >> Importing a name creates a new name in the local scope that refers to > > >> the same object that the imported name referred to. > > > > MRAB is correct here. > > > > > > > Yes, the value of a variable is a reference to an object. Can we not > > > have another round of this right now? > > > > Sure, if you stop spreading misinformation about variables in Python and > > cease the insanity of claiming that the value of a variable is not the > > value you assign to it, but some invisible, unreachable "reference". > > > > x = 999 > > > > The value of x is 999, not some invisible reference. > > > > x = [] > > > > The value of x is an empty list, not some invisible reference. > > We just went through all this. It's clear to me that there are different > ways of looking at these underlying mechanisms, and different people find > truth in different ways of describing them. The virtual world we live in > is complex because of the differing levels of abstraction that are possible. > Some of this disagreement is really a matter of choosing different > abstractions to focus on. > > Most importantly, it's clear to me that we aren't going to come to some > simple consensus, certainly not by throwing around words like "insanity." > > Perhaps at least in this thread we can limit ourselves to addressing the > OP and their question directly, rather than fighting with each other over > which answer is correct? > > --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: the global keyword:
On Saturday, 11 June 2016 19:09:29 UTC-5, MRAB wrote: > On 2016-06-12 00:50, Random832 wrote: > > On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > >> So my question is, how the heck is it possible that I get 5 as the last > >> value printed? the global test_var (global to Test.py) I set to 44 when I > >> ran some_function()??? does anyone have a clue they could throw my way? > > > > Importing a variable from a module copies its value into your own > > module's variable. Updates to the source module's variable will not be > > reflected in your module. > > > Not true. Importing doesn't copy the value. > > Importing a name creates a new name in the local scope that refers to > the same object that the imported name referred to. If that's the case, how is it that I get 5 for test_var??? -- https://mail.python.org/mailman/listinfo/python-list
Re: the global keyword:
On Saturday, 11 June 2016 18:51:11 UTC-5, Random832 wrote: > On Sat, Jun 11, 2016, at 19:44, Marcin Rak wrote: > > So my question is, how the heck is it possible that I get 5 as the last > > value printed? the global test_var (global to Test.py) I set to 44 when I > > ran some_function()??? does anyone have a clue they could throw my way? > > Importing a variable from a module copies its value into your own > module's variable. Updates to the source module's variable will not be > reflected in your module. As I suspected. Nice to have it confirmed. So it copies any imported variable that is of simple type (string, int, float...) What about variables that are user defined classes? Are they referenced or copied? -- https://mail.python.org/mailman/listinfo/python-list
the global keyword:
Hi to all. I have the following file named Solver.py: * from Test import some_function, my_print from Test import test_var some_function() my_print() print(test_var) * and I have the following Test.py: * test_var = 5 def some_function(): global test_var test_var = 44 print("f {0}".format(test_var)) def my_print(): print(test_var) * Would you believe it that when I run Solver.py I get the following output: f 44 44 5 So my question is, how the heck is it possible that I get 5 as the last value printed? the global test_var (global to Test.py) I set to 44 when I ran some_function()??? does anyone have a clue they could throw my way? Much thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Beginner Question
That linked help clear up my confusion...yes you really have to know how things work internally to understand why things happen the way they happen. Thanks again to all -- https://mail.python.org/mailman/listinfo/python-list
Beginner Question
Hi to all I have a beginner question to which I have not found an answer I was able to understand. Could someone explain why the following program: def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2)) print(f(3)) gives us the following result: [1] [1,2] [1,2,3] How can this be, if we never catch the returned L when we call it, and we never pass it on back to f??? Any help is appreciated. Marcin -- https://mail.python.org/mailman/listinfo/python-list