Wiki
FlickrLeetcode
  • 💞Artificial Intelligence
    • ⚙️Midjourney vs Stable Diffusion
    • ⚙️Creative QR Codes with ControlNet
      • ⚙️How to generate a QR Code
      • ⚙️Collect prompt
      • ⚙️clip skip
      • ⚙️AUTOMATIC1111
      • ⚙️Edge detection and human pose detection
    • ⚙️Stable Diffusion
    • ⚙️What is 'token' and 'turbo'?
    • ⚙️Today's learning--LangChain
      • ⚙️Prompt
    • ⚙️LLM Parameters Demystified
    • ⚙️What is Cohere playground?
    • ⚙️DALL-E
    • ⚙️How to use AI to learn something that you don't know?
    • ⚙️Diffusers
    • Boosting Algorithms in machine learning, part 1:AdaBoost
  • 💞GitHub
    • ✅How to add a issue of the code with GitHub?
    • ✅How to edit code?
    • ✅How to use GitHub Desktop
    • ✅How to review in GutHub?
  • 💞Lastest concepts for me
    • 🪅Pandas DataFrame
    • 🪅Choosing between loc and iloc
  • 💞Need to remember
    • 🔢An article for leetcode
    • 🉑two types of group work
    • 🍒What is hashtag#?
    • 🐝Week6_APD
    • 🦋API
    • 🎼BFF
  • 💞Python
    • 🐍argument & parameter
    • 🐍"{:.2f}"
    • 🐍Timeit in Python
    • 🐍Today's learning--Pylint
    • 🐍Split and Strip in Python
    • 🐍Getter and Setter in Python
    • 🐍"import json" in Python
    • 🐍Open CSV file in Python
    • 🐍print(f"An error occurred: {e}")
  • Page
  • 🪅command-line
  • 💞DataVisualization
    • 🪅How to choose plot type
  • 💞DataCleaning
    • 🪅Some basic code of data_cleaning
  • 💞Java
    • 🍡difference use between ArrayList and HashMap in Java
    • 🍡ArrayList & LinkedList
    • 🍡assertFalse(App.checkInputNumber(1,0))
      • 🍡HashSet
    • 🍡iterator
    • 🍡Java concept of assignment 1
    • 🍡Week6_Java
    • 🍡serializable
  • 💞Mark something that easily to forget
    • 🙉Mark something
    • 🙉How to edit cover picture from "Flickr" using "URL" in GitBook?
  • 💞VS Code
    • ✖️Install a project in VS Code
    • ✖️What should do after editing the code of one branch?
    • ✖️How to create a branch in VS code?
    • ✖️How to debug?
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Python

Getter and Setter in Python

Basically, the main purposes of using getter and setters in object-oriented programs is to ensure data encapsulation. Private variables in Python are not actually hidden fields like in other object-oriented languages. Getters and Setters in Python are often used when:

We use getters and setters to add validation logic around getting and setting a value.

To avoid direct access of a class field i.e., private variables cannot be accessed directly or modified by external user.

Using normal function to achieve getters and setters behavior

To achieve getters and setters property, if we define normal get() and set() methods it will not reflect any special implementation.

# Python program showing a use 
# of get() and set() method in
# normal function
class Geek:
    def __init__(self, age = 0):
        self._age = age
        
    # getter method
    def get_age(self):
        return self._age
        
    # setter method
    def set_age(self, x):
        self._age = x
raj = Geek()

# setting the age using setter
raj.set_age(21)

# retrieving age using getter
print(raj.get_age())

print(raj._age)

A getter method is used to retrieve the value of an attribute or property of an object. It is typically used to provide read-only access to an attribute, meaning that the value of the attribute cannot be changed directly. Getter methods are defined using the @property decorator in Python.

PreviousSplit and Strip in PythonNext"import json" in Python

Last updated 1 year ago

Was this helpful?

A setter method, on the other hand, is used to change the value of an attribute or property of an object. It is typically used to provide write-only access to an attribute, meaning that the value of the attribute can be changed but not read. Setter methods are defined using the @<attribute>.setter decorator in Python.

💞
🐍
1
2
3
4
5