Python is fun

Carolina Ramón
4 min readMay 22, 2021

--

Introduction

Python is a powerful general-purpose programming language. It is used in web development, data science, creating software prototypes, and so on. Also Python language is expressive and productive. So you can create solutions quickly and others can understand it easily. One of the fundamental ideas behind Python is to facilitate an easily readable code. The syntax of Python is simple, clean and easy to understand. Unlike many other programming languages, its code is relatively easy to read as it resembles a part of everyday English that we speak. You don’t have to deal with complex syntax in Python.

Python is fun, expressive, its readable style, quick editing, run development cycle meaning you can sit down writing code, rather than fighting compilers and complex syntax.

Object-oriented programming

In this paradigm, programs are modeled around objects that bring together all the functionality related to them. In this way, instead of creating a series of functions without any connection between them, in OOP classes are created, which represent entities that you want to handle in your program. For example, invoices, invoice lines, customers, cars … or any entity that you need to manage conceptually. In that sense, OOP revolves more around data than logic, which is delegated to the background, since it is part of said data, as we will see shortly.

When an instance is created, a new namespace is created, which is initially empty. It clones the class object and attaches all the class attributes. The __init__() is then invoked to create (initialize) instance variables, which are only available to this particular instance.

The built-in function repr(obj) invokes obj.__repr__() if defined; otherwise obj.__str__().

When you inspect an object under the interactive prompt, Python invokes obj.__repr__(). The default (inherited) __repr__() returns the obj's address.

The __str__() is used for printing an "informal" descriptive string of this object. The __repr__() is used to present an "official" (or canonical) string representation of this object, which should look like a valid Python expression that could be used to re-create the object (i.e., eval(repr(obj)) == obj). In our Circle class, repr(c1) returns 'Circle(radius=2.100000)'. You can use "c1 = Circle(radius=2.100000)" to re-create instance c1.

__str__() is meant for the users; while __repr__() is meant for the developers for debugging the program. All classes should have both the __str__() and __repr__().

Class Variable vs. Instance Variables

Class variables are shared by all the instances, whereas instance variables are specific to that particular instance.

Class Variables:

Class variables are defined within the class construction. Because they are owned by the class itself, class variables are shared by all instances of the class. They therefore will generally have the same value for every instance unless you are using the class variable to initialize a variable.

Defined outside of all the methods, class variables are, by convention, typically placed right below the class header and before the constructor method and other methods.

Instance Variables:

Instance variables are owned by instances of the class. This means that for each object or instance of a class, the instance variables are different.

Unlike class variables, instance variables are defined within methods.

id() function in Python

Python contains some built-in classes for data types — integers, floats, strings and so on — . As every instance of a class, those objects have attributes and methods defined by their classes.

Syntax:

id(object)

As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.

Type() funtion in Python

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.

Two different types of arguments can be passed to type() function, single and three argument. If single argument type(obj) is passed, it returns the type of given object. If three arguments type(name, bases, dict) is passed, it returns a new type object.

Syntax :

type(object)
type(name, bases, dict)

Mutable vs Immutable Objects in Python

Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

To summarise the difference, mutable objects can change their state or contents and immutable objects can’t change their state or content.

in the following table you can see the objects that are mutable or not in python

--

--

No responses yet