Python: Built-in Functions
A beginner's guide to some of the most useful functions
Python comes, out of the box with a number of VERY helpful tools known as built-in functions. Here we will discuss some of the functions I found the most helpful and/or used the most as I got started learning the language.
isinstance()
First up is one my of favorite functions and more than likely the one I used the most during the early stages of learning Python. Is instance takes in two arguments, an object and a type, and returns true if the object is an instance of the specified type. Is instance is a great tool for evaluating user input to ensure that it conforms to the data type your application is expecting. For example, a user name input field would expect a string as the type. Is instance can validate that the type is correct before processing the data and completing the function.
@user_name.setter
def user_name(self, new_name)
if isinstance(new_name, str):
self._name = new_name
else:
raise TypeError('User name must be type: String')
hasattr()
Has attribute is another fantastic evaluator function. Has attribute takes in two arguements, an object and an attribute name as a string. Has attribute checks to see if the object contains the specified attribute and returns a boolean value of True or False. This provides you a way to determine if the object has the attribute that you want to manipulate. Back to your example of a user name, you can use hasattr() to check if a user name already exists for the user. If the name attribute exists, you could then raise an AttributeError if name should be immutable or you could prompt the user to use the login feature instead of the signup feature as they already have an account.
@user_name.setter
def user_name(self, new_name)
if not hasattr(self, "_name"):
self._name = new_name
else:
raise AttributeError('User name cannot be changed')
set()
Set is used to convert any iterable sequence in python (like list, or dictionary) into an iterable sequence of distinct elements. Set is very helpful when you need to know or present all of the different types of an attribute without needing to show every instance of that type of attribute. For example, have a Zoo class and that Zoo has many instances of animals. If you wanted to know the different types of animals without seeing how many of each animal the Zoo has, you would use a set.
_repr_
The repr() method returns a more information-rich, or official, string representation of an object. Repr is a great tool, especially early on in python because it allows you to set a more easily read and understandable name for an object or object attribute. I find this incredibly useful for debugging in a tool like ipdb, as seeing <__main__.Example object at 0x102892860> is great to know that an object was created. However it lacks any easily identifiable specificity to ensure that the correct object with the correct attributes was created/edited/etc. Using repr you can create a common language output for the object.
In short, these are just a few of the built-in functions that I found extremely useful while learning Python. They helped me break down walls into areas of understanding, or more easily validate and manipulate objects and attributes. For a more complete list of built-in functions, take a look at the Python documentation here.