Scripting

Python setattr()

Python’s built-in setattr(object, string, value) function takes three arguments: an object, a string, and an arbitrary value. It sets the attribute given by the string on the object to the specified value. After calling the function, there’s a new or updated attribute at the given instance, named and valued as provided in the arguments. For …

Python setattr() Read More »

Python delattr()

Python’s built-in delattr() function takes an object and an attribute name as arguments and removes the attribute from the object. The call delattr(object, ‘attribute’) is semantically identical to del object.attribute. This article shows you how to use Python’s built-in delattr() function. Usage Learn by example! Here’s an example on how to use the delattr() built-in …

Python delattr() Read More »

The Maximum Profit Algorithm in Python

This article presents an algorithmic problem with practical value for stock market analysis. For instance, suppose you are trading the cryptocurrency Ethereum. How much profit in dollars can you make by buying low and selling high based on historical data? Maximum Profit Basic Algorithm The max profit algorithm calculates the maximum profit you’d obtain by …

The Maximum Profit Algorithm in Python Read More »

How to Get The Current Reference Count of an Object in Python?

The reference count is the number of times an object is referenced by a variable. If an object isn’t referenced anymore, it can be safely removed from the memory—what’s the use of an object nobody cares for anyway? This article shows you how to count the number of references to a Python object. What’s the …

How to Get The Current Reference Count of an Object in Python? Read More »

Captive User Interfaces — Why You Should Avoid Them

This tutorial shows you the meaning of captive user interfaces and why they’re discouraged under the Unix philosophy. I’ve written this as a first chapter draft for my upcoming book “From One to Zero” to appear in 2020 with San Francisco-based publisher NoStarch. What’s a Captive User Interface (CUI)? A captive user interface is a …

Captive User Interfaces — Why You Should Avoid Them Read More »

Python exec() — A Hacker’s Guide to A Dangerous Function

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code. Have you ever wondered about the limits of a …

Python exec() — A Hacker’s Guide to A Dangerous Function Read More »

Python complex() — A Useless Python Feature?

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those. This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn …

Python complex() — A Useless Python Feature? Read More »