OBJECTS IN PYTHON
Let’s start by talking about the topic “Objects variables and attributes in Python“. The most important concept to learn to understand the architecture of the entire Python programming language is that in this language everything is an object. The programs that we for example write in the schell as a series of instructions are also objects. It is essential to understand that Python assigns to all these entities, when the program is executed, data structures that are called objects. Objects in Python as already mentioned are data structures that at least contain:
- An identifier assigned by the Python runtime which ensures that that Id is unique throughout the execution of the program.
- A Type that is a type that in the simplest cases can be an int or a string so to speak.
- A value associated with this Type for example literal 20 if the Type is an int or ‘Hello World!’ if the Type is a string.
OBJECTS VARIABLES AND ATTRIBUTES IN PYTHON – I LITERAL
Python has a set of data types that are predefined in the language. By simply indicating the literal value of an int or string in the terminal, Python creates objects from Literals because it can infer the type of these objects at runtime. If we indicate a literal value of 20, Python will infer that it is an int and at runtime it will generate an instance of this int to which it will assign its own identity, the Type that is int and the value 20.
OBJECTS VARIABLES AND ATTRIBUTES IN PYTHON – RETRIEVE TYPE AND IDENTIFIER
OBJECTS VARIABLES AND ATTRIBUTES IN PYTHON –THE VARIABLES
To refer directly to an object in Python we will not use its identity but we will use the variables that consist in assigning a name to an object. To associate a name to an object, the assignment operator is used.
x = 10
This instruction reads like this, creates a literal object of value 10 and assigns this object the name x so that in other parts of the program we can refer to 10 by its name, that is x.
- A valid Python variable name can contain Unicode characters, numbers, letters, and underscores.
- A variable name cannot start with a number.
- It cannot be a reserved word of language
The names assigned to objects in Python are called variables, here you have to be careful. All programming languages have the concept of variable, but in Python it has a much simpler meaning than in other languages. In Python a variable is simply a name that is associated with an object, it is a name that points to an object and very importantly a variable is not an object like in Java and C#.
x = 10
x = ‘Python’
This is the first consequence of how Python handles variables, we have said that the Type refers to the object and not to the variable, so we can first assign an int then a string to x and it is perfectly legal. As Python is dynamically typed, all variables are actually pointers to objects. For example, if a variable is assigned an integer value, a string or a list can be assigned immediately thereafter. On the other hand, objects have a type. Python has various templates / libraries to be used to provide functionality without having to write code, such as the turtle graphics (to draw), copy (to create copies of objects), random (to generate random numbers), sys (to interact) module command line with the interpreter) and time (to operate with time and date units).
BASIC TYPES
Python provides a large number of basic types, essentially numeric and container types. Distinctive feature is the native support, in addition to classic types such as integers, floating point (numbers with floating point) and alphanumeric strings, even more advanced types such as integers of arbitrary size, complex numbers, lists, sets and dictionaries. However, there is no specific type for characters. Many other types are importable from standard libraries and new types can be created through classes.
CALLABLE OBJECT
We make a distinction between objects that are not callable and objects that are (Callable). Non callable objects are those that usually represent data containing values or even multiple values as in lists. Callable objects do not represent data but executable code. The most common callable objects are functions. Let’s see an example.
THE ATTRIBUTES
We have seen that the values of an object can be single or multiple, moreover the values are a direct consequence of the Type of the object, they have their own identity and we add that objects in Python have a series of attributes. These attributes can be data or functions. When the attribute is a function this is called a method. To access the attributes of an object, dot-notation is used.
a.b a.b()
where a is the object b in the first case represents a data, in the second a method. Writing with dot notation the code returns the value of the data (if the attribute is a data) or executes some code if the attribute is a Callable object that is a method. In this case we must specify a pair of round brackets if the method has no parameters, otherwise we indicate the parameters to be supplied to the method. Let’s see an example upper() which is a predefined function that all strings have and which transforms the contents to uppercase.
DEEPENING
In Python, the concepts of objects, variables and attributes are fundamental to understanding object-oriented programming (OOP). Here is a description of each:
Objects
In Python, everything is an object. An object can be considered an instance of a class. Classes define the “type” of an object by determining what attributes (properties) and methods (functions) an object of that class possesses.
class Cane:
def __init__(self, nome, razza):
self.nome = nome
self.razza = razza
def abbaia(self):
return “Woof!“
mio_cane = Cane(“Fido“, “Golden Retriever“)
In this example, mio_cane is an object of the class Cane.
Variables
A variable is a name that refers to a value in memory. In Python, it is not necessary to declare the type of a variable: the type is automatically determined by the value assigned. Variables can be used to store references to objects.
Example:
x = 10
name = “Alice”
mio_cane = Cane(“Fido,” “Golden Retriever”)
Here, x, name and mio_cane are variables that refer to an integer, a string and a Cane object, respectively.
Attributes
Attributes are properties or variables associated with an object. Attributes can be of two types:
– Instance attributes: These attributes are defined within the __init__ method of a class and belong to the object instance.
– Class attributes: These attributes are shared among all instances of a class.
Example:
class Cane:
specie = “Canis lupus familiaris” # Attributo di classe
def __init__(self, nome, razza):
self.nome = nome # Attributo di istanza
self.razza = razza # Attributo di istanza
In this example, species is a class attribute, while name and race are instance attributes.
Using Attributes
You can access the attributes of an object using the dot (.) notation:
print(mio_cane.nome) # Output: Fido
print(mio_cane.razza) # Output: Golden Retriever
print(mio_cane.specie) # Output: Canis lupus familiaris
Conclusion
-Objects: Instance of a class that contains data (attributes) and functionality (methods).
-Variables: Names that refer to values or objects in memory.
-Attributes: Properties associated with an object, defined within a class. They can be instance or class attributes.
These concepts work together to enable the construction and manipulation of complex data structures in Python, facilitating flexible and maintainable software development.
Leave A Comment