1.1 Python objects and methods - nealtran1905/PythonForResearch GitHub Wiki

Python contains many data types as part of the core language. It's important to understand that all data in a Python program is represented by objects and by relationships between objects. Here is the idea - the value of some objects can change in the course of program execution. Objects whose value can change are said to be mutable objects, whereas objects whose value is unchangeable after they've been created are called immutable. Python also contains building functions that can be used by all Python programs. The Python library consists of all core elements, such as data types and built-in functions but the bulk of the Python library consists of modules. In order for you to be able to make use of modules in your own code, you first need to import those modules using the import statement.

Each object in Python has three characteristics.

These characteristics are called object type, object value, and object identity. Object type tells Python what kind of an object it's dealing with. A type could be a number, or a string, or a list, or something else. Object value is the data value that is contained by the object. This could be a specific number, for example. Finally, you can think of object identity as an identity number for the object. Each distinct object in the computer's memory will have its own identity number. Most Python objects have either data or functions or both associated with them. These are known as attributes. The name of the attribute follows the name of the object. And these two are separated by a dot in between them. The two types of attributes are called either data attributes or methods. A data attribute is a value that is attached to a specific object. In contrast, a method is a function that is attached to an object. And typically a method performs some function or some operation on that object.

Object type always determines the kind of operations that it supports.

In other words, depending on the type of the object,

different methods may be available to you as a programmer.

Finally, an instance is one occurrence of an object. For example, you could have two strings. They may have different values stored in them, but they nevertheless support the same set of methods. To clarify the difference between a data attribute and a method, let's do some Python. Let's first import a library called NumPy, which I'm going to import as np and this is just to save me some typing. I'm going to define two arrays. Let's say my first array consists of numbers 1, 3, and 5. I'm going to call that x. And my second array is going to be called y and let's say it consists of numbers 1, 5, and 9. Now both of these objects are NumPy arrays. That means they both support the same methods and have the same attributes available. If I wanted to know the mean of the numbers in x, I would simply ask Python to return that. Here, I'm calling the mean method, which is connected to the object x. I can do the same exact thing for object y. They turn out to have different means in this case, but in both cases the mean method is available to me. Let's look at an example of a data attribute. If I type x.shape, Python is telling me that this array contains three numbers. I can similarly ask, how many elements do I have in y? And the answer is the same. There are three elements embedded in y. Notice in the first case, when I ask Python to compute a value, I have parentheses following the name of the attribute. That means mean is a function. In contrast, when I ask Python to return the shape of the array, I don't have parentheses following the word "shape". That means shape is a data attribute.