Untitled-3

Useful Python Functions and Features You Need to Know



Even after using Python for years, we stumble upon functions and features that we did not know about. Some of these can be quite useful, yet underused. With that in mind, I’ve compiled a list of incredibly useful Python functions and features that you should be familiar with.

Functions with Arbitrary Number of Arguments

You may already know that Python allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.

First, here is an example with just optional arguments:

Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize Tuples:

Using Glob() to Find Files

Many Python functions have long and descriptive names. However it may be hard to tell what a function named glob() does unless you are already familiar with that term from elsewhere.
Think of it like a more capable version of the listdir() function. It can let you search for files by using patterns.

You can fetch multiple file types like this:

If you want to get the full path to each file, you can just call the realpath() function on the returned values:

Debugging

Some of the examples below make use of the inspect module. This module can be very useful for debugging purpose and you can get with it much more that what described here.
We are not going to cover each one of these in this article, but I will show you a few use cases.

Generating Unique ID’s

There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it’s not exactly meant for this purpose

There is actually a Python function named uuid() that is meant to be used for this.

You may notice that even though the strings are unique, they seem similar after several characters. This is because the generated string is related to the computer network address.

To reduce the chances of getting a duplicate, you can use this two functions.

Serialization

Have you ever needed to store a complex variable in a database or a text file? You do not have to come up with a fancy solution to convert your arrays or objects into formatted strings, as Python already has functions for this purpose.

This was the native Python serialization method. However, since JSON has become so popular in recent years, they decided to add support for it. Now you can decode and encode as well in JSON:

It is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.

Compressing Strings

When talking about compression, we usually think about files, such as ZIP archives. It is possible to compress long strings in Python, without involving any archive files.

Register Shutdown Function

There is a module called atexit, which will let you execute some code right before the script finishes running.

Imagine that you want to capture some benchmark statistics at the end of your script execution, such as how long it took to run:

At first this may seem trivial. You just add the code to the very bottom of the script and it runs before it finishes. if there is a fatal error, or if the script is terminated by the user, again it may not run.

When you use atexit.register(), your code will execute no matter why the script has stopped running.

Conclusion

Are you aware of any other Python features that are not widely known but can be quite useful? Please share with us in the comments. And thank you for reading!


  • http://keturn.net/ keturn

    Regarding uuid.uuid1, you say “To reduce the chances of getting a duplicate, you can use this two functions.”

    Under what circumstances are you worried about uuid.uuid1 returning duplicates? for a uuid1 to collide, you’d have to generate them with the same network address, at the same time *and* have the remaining random 14 bits collide. The docs could be a little more detailed about what “same time” means, but it looks like it’s probably on the order of milliseconds, at worst. (I’ll leave the chances of calculating the odds of a “birthday paradox” collision in a space of 16,384 choices per millisecond as an exercise to other readers.)

    If you do want more randomness — or, more likely, you don’t want to leak information about your network address or the current time in your unique identifier — that’s what the uuid.uuid4 function is for.

    Your use of SHA hashes is going to return results that are only as random as the inputs you give them, and (’1′, ‘a’) is not very random. =)

  • OpenWatch

    Best debugger ever:

    import pdb
    pdb.set_trace()

    Njoi!

  • craigaddyman

    I just used glob recently to keep my desktop tidy.

    files = glob.glob(‘*.*’) Using the wildcards for both files names and extensions to collect all files.

    Then searched each file name for client names to send them to their respective folders – http://www.craigaddyman.com/keeping-your-desktop-tidy-like-a-boss/

  • ca77y

    >>> (‘a’ or ‘x’) in ['a', 'b']
    True
    >>> (‘a’ and ‘x’) in ['a', 'b']
    False
    >>> (‘a’ and ‘b’) in ['a', 'b']
    True

    • ezabolotniy

      Very bad example…

      >>> (‘a’ or ‘b’) in ['a', 'b']
      True
      >>> (‘c’ or ‘b’) in ['a', 'b']
      False

      It’s no magic here, but someone can mistakenly use it:
      >>> (‘a’ and ‘b’)
      ‘b’
      >>> (‘a’ or ‘b’)
      ‘a’

      • Matt Stromberg

        Very nice catch.

      • ca77y

        /facepalm
        me fails… at least it looks deceptively good

  • Ian E.

    Minor bug in the first example. The second print statement should probably be “arg2″ and the expected output is not correct. As written, it’s:

    # prints args1: Hello
    # prints args1: World

    Learned about glob and atexit. Thanks!

    • ajkumar25

      Thanks, fixed that.

  • Tamir Lousky

    Nice list of tips and techniques, thanks for sharing!

  • dEMON_hUNTER

    perfect !