The inspect library in Python

Todays post will be on the inspect library in python that can be used as a way to inspect live objects. Some examples of live objects are modules, classes, methods of classes, stand alone functions. There are also tracebacks, and frame objects that can eb used as a way to examining the state of the python interpreter.

The inspect library is packed with helpful methods for getting the members of a module, and also to find out if something is even a module to begin with. In addition there are many more additional such methods that have to do with extracting source code, and checking the state of frame objects.

This is a library that seems to show up in a lot of basic python examples for various things, so it makes sense to take a moment to take a deeper look into the module as a whole.

Read More

The sys library in Python

The sys library in python seems to come up a lot in code examples, so it would make sense to write a post on this library. This library has a lot of operating system level features to work with, but it is not really a replacement for the os library that also comes up a lot in examples.

One major feature is that this library can be used as a way to get any positional arguments that might have been passed to the script when it was called in the form of a list. This is one of the first things I like to learn how to do when it comes to learning a new programing language, at least when it comes to a language and environment that can be used to create scripts that can be called in a command line interface. When making such scripts there should be a way to access and positional arguments that may have been passed when the script was called. In nodejs there is the process.argv array in the process global, in bash scripts there are special parameters than can be used, and in python the way to do so is with, you guessed in the sys standard libraries argv list.

However there are a number of other features in the sys library that are also worth looking into with a few quick code examples. I will not be going over every little detail here, however I will of course be covering the most striking features that are worth writing about in detail of course, so lets go over them.

Read More

The OS library in Python

The os standard library in python is a library that contains some operating system dependent functionality. There are a few other libraries that come to mind that can also be used as a way to make use of operating system dependent features. For example the subprocess library can be used to call a command on the host operating system, but before doing so it helps to know what operating system you are working with first. So the os standard library is a good staring point when it comes to checking out what kind of system my code might be running on top of.

On top of basic functions and properties to find out what kind of operating system python is running on there are also a number of functions and methods that have to do with paths, and basic file io. The file io features are not a replacement for the open built in function which is what I would be more interested in using for most basic examples and projects anyway. If for some reason I want or need to do file io by way of getting a file descriptor first, and then passing that file descriptor to a function to open a file object, and more with file io in a more professional way, then the file io methods in the os library can be used to do that kind of file io.

Read More

Shallow and deep Copying of objects in Python

The copy standard library in python is yet another standard library in python that might prove to be imporant for most typical projects. I say that speaking from experence in other languages at least, often I am in a situation in which I want to have an indepedant copy of an object. That is having a complate copy s source object that I can then mutate, without effecting the source object. So I should have one way or another to copy objects in python, and one good option seems to be the copy built in standard library.

When it comes to making a copy of a string or number value there is no big fuess when it comes to copying thiose kinds of values. An indepedant value can be created with just and assignment operator, as such some might exspect that the same can be done with objects. However in python, and many otherlnagaues such as javaScript for that matter, this is not the case. The assignment operator will just create another reference or binding if you prefer to the same object in memory. To make a true copy of an object there is a need for spechial functions that will create and return a true indepedant copy of an object then.

Copying an object is not always so quick and simple, there is copying just the primative values of an object which will result in a so called shallow copy of an object, and then there is also copying any additional nested objects which would result in a deep clone.

Read More

The Array library in Python

Built into python itself is the list data type, and for most situations this seems to work well as an array in python. However it is not like lists are the only option when it comes to arrays, or sequence types in python, another built in feature is arrays. Arrays might not be there to begin with, but they can quickly be added by way of the array standard library. Lists might still work just fine in most situations, however I think I should take a moment to at least touch base on these when it comes to being aware at least of an alternative to lists, and the other sequence types that are built into python itself.

Read More