Please try to adopt the inline reply style; we prefer it here. It lets us reply
point by point and makes messages read like conversations. Anyway...
On 07Sep2018 23:57, Chip Wachob <wach...@gmail.com> wrote:
Point taken on 'bytes'.. thanks.
the
scratch_ary = bytearray()
was my attempt at 'setting' the type of the variable. I had hoped
that it would help resolve the error messages telling me that the
types didn't go together.
Coming from a 'C' background, I find the lack of typing in Python to
be confusing. I'm used to working with bytes / words signed and
unsigned for a reason.
Ok. Variables aren't typed. Variables are references to objects, which _are_
typed. So pointing a variable at a bytearray doesn't set its type in any
persistent sense.
Thus:
x = 1
x = "a string"
x = bytearray()
All just point "x" at different objects. If you'd like a C metaphor, variables
are _like_ (void*) pointers: they can reference any kind of object.
Aside: they're not pointers, avoid the term - people will complain. As far as
the language spec goes they're references. Which may in a particular
implementation be _implemented internally_ using pointers.
So, if I'm understanding the transfer() function correctly, the
function takes and returns a bytearray type.
It would be good to see the specification for the transfer function. They we
can adhere to its requirements. Can you supply a URL?
You mentioned about
constructing a bytearray if I need one. Can you expand on how I
approach that?
Well, you were getting several bytes or bytearray objects back from transfer
and wanted to join them together. The efficient way is to accumulate them in a
list and then join them all together later using the bytearry (or bytes) .join
method:
https://docs.python.org/3/library/stdtypes.html#bytearray.join
So:
data_list = []
for ....
# send some data, get some data back
data = transfer(...)
# add that buffer to the data_list
data_list.append(data)
# make a single bytes object being the concatenation of all the smaller data
# chunks
all_data = bytes.join(data_list)
It looks to me like your transfer() function is handed a bytes or bytearray and
returns one. Normally that would be a separate bytes or bytearray.
Aside: bytes and bytearray objects are generally the way Python 3 deals with
chunks of bytes. A "bytes" is readonly and a bytearray may have its contents
modified. From a C background, they're like an array of unsigned chars.
I'm going to try the experiment you mentioned in hopes of it giving me
a better understanding of the 'types' and what happens with the
variables.
Sounds good.
Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor