is there a way to know the length of a bytearray variable in python?

2024/9/29 8:22:55

I have this code:

variable = "FFFF"
message = bytearray( variable.decode("hex") )

after this, I want to perform something like this:

message.len()

but it seems that bytearray does not have something like "len()" implemented, is it possible to know the lenght?

Answer

The idiom used for this in python is len(obj), not obj.len()

>>> v = 'ffff'
>>> msg = bytearray(v.decode('hex'))
>>> len(msg)
2

If you are creating your own class that should be able to report its length, implement the magic method __len__(self), which will be called when the built-in function len() is called and passed an instance of your class.

Why?

See this old post from Guido:

(a) For some operations, prefix notation just reads better thanpostfix — prefix (and infix!) operations have a long tradition inmathematics which likes notations where the visuals help themathematician thinking about a problem. Compare the easy with which werewrite a formula like x*(a+b) into xa + xb to the clumsiness ofdoing the same thing using a raw OO notation.

(b) When I read code that says len(x) I know that it is asking for thelength of something. This tells me two things: the result is aninteger, and the argument is some kind of container. To the contrary,when I read x.len(), I have to already know that x is some kind ofcontainer implementing an interface or inheriting from a class thathas a standard len().

https://en.xdnf.cn/q/71156.html

Related Q&A

Matplotlib: Check for empty plot

I have a loop which loads and plots some data, something like this:import os import numpy as np import matplotlib.pyplot as pltfor filename in filenames:plt.figure()if os.path.exists(filename):x, y = n…

Hyperlink in Streamlit dataframe

I am attempting to display a clickable hyperlink inside a dataframe containing filtered results on Streamlit. This is my code so far: import pandas as pd import streamlit as st import openpyxl import n…

Best way to detect if checkbox is ticked

My work:Scan the paper Check horizontal and vertical line Detect checkbox How to know checkbox is ticked or notAt this point, I thought I could find it by using Hierarchical and Contours: Below is my w…

ResultSet object has no attribute find_all

i always met one problem, when I scraping one web page.AttributeError: ResultSet object has no attribute find. Youre probably treating a list of items like a single item. Did you call find_all() when y…

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…