Recipient and Installer in Python

Python Methods and Functions

Python's getter and setter methods are different from other object-oriented programming languages. In fact, the main purpose of using getter and setter methods in object-oriented programs is to provide encapsulation of data. 

# Python program showing usage
# get () and set () methods in
# normal function

 

class Geek:

def __ init __ ( self , age = 0 ):

self ._ age = age

 

  # method get

def get_age ( self ):

return self ._ age

 

# installation method

def set_age ( self , x):

self ._ age = x

 

raj = Geek ()

  
# setting age using setter

raj.set_age ( 21 )

 
# getting age using a getter

print (raj.get_age ())

 

print (raj._age)

Exit :

 21 21 

In the above code, the functions get_age () and set_age () acts like normal functions and does not play any role as getters and setters, to achieve this functionality Python has a special property () function property () .

Using the property () function to achieve Getter and Setter Behavior

In Python, property () is a built-in function that creates and returns a property object. The property object has three methods: getter (), setter (), and delete (). The property () function in Python has property (fget, fset, fdel, doc) four arguments " property (fget, fset, fdel, doc) , fget — this is the function to get the value of an attribute.  fset — this is the function to set the value of an attribute.  fdel — this is the function to remove the value of an attribute.  doc creates a doc string for the attribute. The property object has three methods: getter () , setter () , and delete () to fget specify fget , fset , and fdel . For example

# Python program showing
# using the property () function

 

class Geeks:

def __ init __ ( self ):

self ._ age = 0

 

# function to get the value of _age

def get_age ( self ):

print ( " getter method called " )

return self ._ age

 

  # function to set _age value

def set_age ( self , a):

print ( " setter method called " )

  self ._ age = a

  

  # function to remove the _age attribute

def del_age ( self ):

del self ._ age

 

age = property (get_age, set_age, del_age) 

 

mark = Geeks ()

  

mark.age = 10

 

print (mark.age)

Output:

 setter method called getter method called 10 

In the above code, there is only one print statement in line # 25 but the output is three lines because of the set_age () called in line # 23 and the get_age () method called in line # 25 . Therefore, age — it is a property object that helps preserve access to the private variable.

Using @property decorators to achieve getter and setter behavior

In the previous method, we used the property () function to achieve the behavior of getters and setters. However, as mentioned earlier in this post, getters and setters are also used to validate getting and setting attribute values. There is another way to implement the property function, i.e. using the

# Python program showing usage
# @ property

 

class Geeks:

def __ init __ ( self ):

self ._ age = 0

 

# using a decorator properties

# get function

  @ property

def age ( self ):

print ( "getter method called " )

  return self ._ age

 

# setter

@ age . setter

  def age ( self , a):

  if (a & lt;  18 ):

  raise ValueError ( "Sorry you age is below eligibility criteria" )

print ( " setter method called " )

  self ._ age = a

 

mark = Geeks ()

 

mark.age = 19

 

 

print (mark.age)

Exit :

 setter method called getter method called 19 

In the above The code makes it clear how to use the @property decorator to create getter and setter methods in a pythonic way.  Line 15-16 acts like validation code that raises a ValueError if we try to initialize age to a value less than 18. So any validation method can be used in getter or setter functions.



Get Solution for free from DataCamp guru