## Definition A public [[method]] that returns the value of one of its object's attributes. *Noun.* > Instead of exposing the data directly, make a getter method to safely retrieve the account balance. ## Characteristics - Used in [[object-oriented programming]]; - provides read-only access to a specific attribute without modifying the object's [[object state|state]]; - returns the attribute's value without altering it; - supports [[encapsulation]] by providing controlled access to hidden data; - typically used to return the value of a [[private]] attribute; and - has an identifier that is, by convention, prefixed with `get`, e.g., `get_balance()`. ## Examples ### `get_id()` ```python class Student: def __init__(self, p_name, p_id): self.name = p_name self.__id = p_id # getter def get_id(self): return self.__id ``` ## Non-examples ### Accessing a public attribute directly Directly accessing a public attribute is not a getter as it does not involve the use of a method. ```python class Student: ... shazia = Student("Shazia", 17) print(shazia.name) # does not use a getter ``` ### Methods that derive a value A getter returns the value of an attribute and does not derive anything. ```python class Student: ... def derive_username(self): return f"{self.__id}{self.name[:8]}" # derives a return value ``` ### Methods that modify state Any method that modifies an object's state is not a getter, including [[setter|setters]]. ```python class Student: ... def set_id(self, p_id): self.__id = p_id # modifies state ```