First-Class Citizens in Python

PythonServer Side ProgrammingProgramming

First-class citizens are entities that enable support for all the operations facilitating other fellow entities.

These entities are often used : while passing an argument , returning a value from function , conditional modifications & value assignment.

In this article, we will look at the implementation & usage of first-class citizens in Python 3.x or earlier. Also, we will be learning what all entities get the tag of being First Class citizens.

These citizens include Variables along with functions.

Let’s first get familiar with the data types that are part of First-class citizens

  • Integers
  • Floating type
  • Complex Numbers
  • Strings

All four types mentioned above are given the tag of being first-class citizens in Python 3.x. Or earlier.

Example

#Declaration of an integer
print("hello world")
int_inp=int(input())
print("This is a First class Citizen of "+str(type(int_inp)))
#Declaration of floating type
float_inp=float(input())
print("This is a First class Citizen of "+str(type(float_inp)))
#Declaration of complex numbers
complex_inp=complex(input())
print("This is a First class Citizen of "+str(type(complex_inp)))
#Declaration of Strings
str_inp=input()
print("This is a First class Citizen of "+str(type(str_inp)))

Input

2
23.4
4+7j
tutorialspoint

Output

This is a First class Citizen of <class 'int'>
This is a First class Citizen of <class 'float'>
This is a First class Citizen of <class 'complex'>
This is a First class Citizen of <class 'str'>

Now Let’s take a look at some functions which are referred to as first class citizens

First class objects are uniformly handled in Python language. Being objectoriented every entity refers to a default object which can be referenced and dereferenced at any point of time. Storage may be done using data structures or control structures.

Now we will look whether python supports first-class functions or not. So any language is said to support first-class functions when it treats functions as firstclass objects.

Example

# Python program
# functions being be treated as objects
def comp_name(text):
   return text.isupper()
print(comp_name("TUTORIALSPOINT"))
new_name = comp_name #referencing a function with the object
print(new_name("TutorialsPoint"))

Output

True
False

Example

# Python program
# functions being passed as arguments to other functions
def new_inp(text):
   return text.upper()
def old_inp(text):
   return text.lower()
def display(func):
   # storing the function in a normal variable
   code = func("Love Coding, Learn everything on Tutorials Point")
   print (code)
display(new_inp) #directly referenced by passing functions as arguments.
display(old_inp)

Output

LOVE CODING, LEARN EVERYTHING ON TUTORIALS POINT
love coding, learn everything on tutorials point

Here it is clearly seen that Python functions can be referenced using an object and can also be passed as an argument to another function which clearly shows that in Python functions are First Class Citizens and can be referenced and dereferenced using an object entity.

Conclusion

In this article, we learnt the implementation of max and min function included in the Standard Python Library.

raja
Published on 29-Aug-2019 06:37:15
Advertisements