Python class function inside function

Python Objects and Classes

In the last tutorial, we learned about Python OOP. We know that python also supports the concept of objects and classes.

An object is simply a collection of data (variables) and methods (functions). Similarly, a class is a blueprint for that object.

Before we learn about objects, let’s first know about classes in Python.

Python Classes

A class is considered as a blueprint of objects. We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

Since many houses can be made from the same description, we can create many objects from a class.

Define Python Class

We use the class keyword to create a class in Python. For example,

class ClassName: # class definition 

Here, we have created a class named ClassName .

class Bike: name = "" gear = 0
  • Bike — the name of the class
  • name/gear — variables inside the class with default values «» and 0 respectively.

Note: The variables inside a class are called attributes.

Python Objects

An object is called an instance of a class. For example, suppose Bike is a class then we can create objects like bike1 , bike2 , etc from the class.

Here’s the syntax to create an object.

# create class class Bike: name = "" gear = 0 # create objects of class bike1 = Bike()

Here, bike1 is the object of the class. Now, we can use this object to access the class attributes.

Access Class Attributes Using Objects

We use the . notation to access the attributes of a class. For example,

# modify the name attribute bike1.name = "Mountain Bike" # access the gear attribute bike1.gear

Here, we have used bike1.name and bike1.gear to change and access the value of name and gear attribute respectively.

Example 1: Python Class and Objects

# define a class class Bike: name = "" gear = 0 # create object of class bike1 = Bike() # access attributes and assign new values bike1.gear = 11 bike1.name = "Mountain Bike" print(f"Name: , Gears: ")
Name: Mountain Bike, Gears: 11

In the above example, we have defined the class named Bike with two attributes: name and gear .

We have also created an object bike1 of the class Bike .

Finally, we have accessed and modified the attributes of an object using the . notation.

Create Multiple Objects of Python Class

We can also create multiple objects from a single class. For example,

# define a class class Employee: # define an attribute employee_id = 0 # create two objects of the Employee class employee1 = Employee() employee2 = Employee() # access attributes using employee1 employee1.employeeID = 1001 print(f"Employee ID: ") # access attributes using employee2 employee2.employeeID = 1002 print(f"Employee ID: ")
Employee ID: 1001 Employee ID: 1002

In the above example, we have created two objects employee1 and employee2 of the Employee class.

Python Methods

We can also define a function inside a Python class. A Python Function defined inside a class is called a method.

# create a class class Room: length = 0.0 breadth = 0.0 # method to calculate area def calculate_area(self): print("Area of Room constructor">Python Constructors 

Earlier we assigned a default value to a class attribute,

class Bike: name = "" . # create object bike1 = Bike()

However, we can also initialize values using the constructors. For example,

class Bike: # constructor function def __init__(self, name = ""): self.name = name bike1 = Bike()

Here, __init__() is the constructor function that is called whenever a new object of that class is instantiated.

The constructor above initializes the value of the name attribute. We have used the self.name to refer to the name attribute of the bike1 object.

If we use a constructor to initialize values inside a class, we need to pass the corresponding value during the object creation of the class.

Here, "Mountain Bike" is passed to the name parameter of __init__() .

Table of Contents

Источник

How to Use Nested Functions in Python

This article will cover a guide on using nested functions in Python. Nested functions or inner functions are defined inside other Python functions. They are useful in certain programming patterns and use cases. Some of them will be explained in this article. All code samples in this article are tested with Python 3.9.5 on Ubuntu 21.04.

About Nested / Inner Functions

Nested functions, as the name suggests, are Python functions that are created inside other Python functions. Besides its own scope, the inner function has access to the objects available in the scope of the outer function. The inner function can be termed as a single Python object with its own data and variables. This inner function is protected by the outer function and cannot be called or referred from the global scope. This way the inner function acts as a hidden entity that works within the boundaries of outer function only and global scope remains unaware of it. This process is also known as “encapsulation” in programming. Here is an example of a nested function in Python.

def visibile_outer_function ( name ) :
def hidden_inner_function ( ) :
print ( name )
hidden_inner_function ( )

visibile_outer_function ( "John" )
hidden_inner_function ( )

The outer function takes one mandatory argument called “name”. The inner function has access to the scope of the outer function so it can make use of the name variable. A call to the inner function is then made in the outer function. Next, a call to both inner and outer functions is made in the global scope. After running the above code sample, you should get the following output:

John
Traceback ( most recent call last ) :
File "main.py" , line 9 , in
hidden_inner_function ( )
NameError : name 'hidden_inner_function' is not defined

As you can see in the output, the outer function works fine when you call it from global scope. An error is thrown when you try to call the inner function as no such thing is available in the global scope.

Inner Functions Use Cases

Now that you have some understanding about nested functions, you may wonder about their utility and when to use them. One of the most common uses of inner functions is for creating helper functions within the main function. Inner functions can also be used as decorators and can be used to implement closures in your program. These use cases are explained below with examples.

Creating a Helper Function

Helper functions are like any other Python functions, but they are called “helper” functions because they can help better organize complex code and can be reused any number of times to avoid code repetition. Below is a code sample that illustrates an inner helper function.

def get_ticket_price ( name ) :
members = [ "Tony" , "Peter" , "Mark" ]
price = 10

def get_discounted_price ( discount = 1.0 ) :
return ( price * discount )

if name in members:
ticket_price = get_discounted_price ( discount = 0.50 )
else :
ticket_price = get_discounted_price ( )
print ( "Ticket price for " + name + " is: $" + str ( ticket_price ) )

get_ticket_price ( "Tony" )
get_ticket_price ( "John" )

The main callable outer function is “get_ticket_price”. It takes the name of a person as the mandatory argument. The function “get_discounted_price” is an inner helper function that takes “discount” as an optional argument. The list “members” contains names of all registered members who are eligible for a discount. A discounted price for members is calculated by calling the inner function and supplying at it a discount value as an argument. This helper function can be called multiple times based on requirements and you can also change the logic within the inner function. Thus inner helper functions allow you to simplify code and avoid unnecessary repetition. After running the above code sample, you should get the following output:

As you can see in the output above, Tony gets a discount on ticket price as he is in the members list.

Implementing Closures

Closures are instances of inner functions that are returned by outer functions. These inner functions have access to the scope of outer functions and they continue to have access to the scope of outer function even after the outer function has stopped executing. Have a look at the code sample below:

def get_discounted_price ( price ) :
def discounted_price ( discount ) :
return price * discount
return discounted_price

first_discount = get_discounted_price ( 10 )
second_discount = get_discounted_price ( 10 )
print ( first_discount ( 0.50 ) )
print ( second_discount ( 0.60 ) )

The outer function “get_discounted_price” returns a reference to the inner function called “discounted_price”. Notice that in the return statement, the function is called without braces. Next, two new instances called “first_discount” and “second_dicount” are created by calling the outer function and a value for “price” argument is supplied to these calls. At this point of time, the outer function has finished executing but its state has been saved in the first_discount and second_discount objects. Now when you call the first_discount and second_discount instances with braces and arguments, they will already have access to a variable called price along with its value. The argument supplied to these instances now goes to the inner function which then returns a result.

After running the above code sample, you should get the following output:

Closures are generally used in situations where your program requires preserving the state of a function.

Creating Decorating Functions

Decorator functions in Python modify behavior of an existing Python function without changing it. So when you attach a decorator to a function, you can add additional functionality to the function or modify its behavior while keeping its original behavior intact. A typical Python decorator looks like this:

Here “@decorator” will modify the behaviour of the “decorated” function. You can create decorator functions using nested functions. To create a decorator, define a function and pass it to an outer function as an argument. This passed function is then called within another inner function where you can use it and implement logic. Finally the outer function returns the inner function which contains the modified behavior. Take a look at the code sample below.

def get_discounted_price ( amount ) :
def discounted_price ( ) :
price = amount ( )
new_price = price * 0.50
return new_price
return discounted_price

The outer function “get_discounted_price” is passed another function called “amount” as an argument. The inner function makes use of the passed function and adds a certain behavior to it. The outer function then returns a reference to the inner function that contains the modified behavior. After defining the decorator, you can call the it in following way:

@ get_discounted_price
def get_price ( ) :
return 10

Decorators are attached to functions whose behavior you are trying to modify. They always start with the “@” symbol. By using the decorator here, you are passing the “get_price” function to the “get_discounted_price” function as an argument. Now when you call the get_price function, you won’t get 10 as output but a number modified by the get_discounted_price decorator. After running the above code sample, you should get the following output:

The decorator usage shown above is equivalent to the following code:

def get_discounted_price ( amount ) :
def discounted_price ( ) :
price = amount ( )
new_price = price * 0.50
return new_price
return discounted_price

def get_price ( ) :
return 10

final_price = get_discounted_price ( get_price )
print ( final_price ( ) )

Instead of using a “@decorator” syntax as a shorthand, you can simply create a new instance of the outer function and supply it another function as an argument. End result of both coding patterns is the same. Since decorators keep the behavior of the original function intact, they are really useful if you want to call them on a case by case basis and at the same time preserve the vanilla implementation of a decorated function.

Conclusion

You can use nested functions in a variety of ways to create inner functions that add extra functionality and logic to the outer function. Some of the most common use cases for nested functions have been explained in the article. You can also create your own implementations of inner functions, as all functions are treated as first class objects in Python and they can be returned or passed as arguments.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.

Источник

Читайте также:  Пример javascript сложный код
Оцените статью