Public in Python

In Python, the term "public" refers to the accessibility of methods and attributes within a class. Public methods and attributes are those that can be accessed from outside the class, forming the interface through which other parts of a program can interact with the class's functionality. This is a fundamental concept in object-oriented programming, allowing for the manipulation and retrieval of an object's data in a controlled manner.

Public Methods and Attributes

Public methods and attributes are the default in Python, meaning that unless specified otherwise, all methods and attributes are public. This allows any code with access to an instance of a class to interact with these methods and attributes.

Example of Public Methods and Attributes

Consider the following example of a class with public methods and attributes:

class Car:
    def __init__(self, make, model):
        self.make = make  # Public attribute
        self.model = model  # Public attribute

    def display_info(self):  # Public method
        print(f"Car make: {self.make}, Model: {self.model}")

# [Creating an instance of Car](https://livebook.manning.com/book/python-how-to/chapter-8/140)
my_car = Car("Toyota", "Corolla")

# [Accessing public attributes](https://livebook.manning.com/book/python-how-to/chapter-8/140)
print(my_car.make)  # Output: Toyota
print(my_car.model)  # Output: Corolla

# [Calling a public method](https://livebook.manning.com/book/python-how-to/chapter-8/140)
my_car.display_info()  # Output: Car make: Toyota, Model: Corolla

In this example, the make and model attributes, as well as the display_info method, are public. They can be accessed and modified directly from outside the class.

Characteristics of Public Members

  • Accessibility: Public members can be accessed from anywhere in the program where the class instance is available.
  • Modification: They can be modified directly without any restrictions.
  • Visibility: Public members are visible to all parts of the program, making them suitable for attributes and methods that are intended to be used by other parts of the code.

Public members are a fundamental part of object-oriented programming in Python, providing a straightforward way to interact with objects and their data.

Public Methods

Public methods are functions defined within a class that can be called from outside the class. These methods are part of the class's interface and are designed to perform operations or return information about the object.

Example of a Public Method

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def start_engine(self):
        print(f"The engine of the {self.make} {self.model} is now running.")

# [Usage](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61)
my_car = Car("Toyota", "Corolla")
my_car.start_engine()  # This is a public method call

In this example, start_engine is a public method that can be accessed from outside the Car class. It allows the user to start the car's engine, demonstrating how public methods enable interaction with an object's functionality.

Public Variables

Public variables, also known as attributes, are data members of a class that can be accessed directly from outside the class. These variables are part of the class's interface and can be used to store and retrieve information about the object.

Example of a Public Variable

class Car:
    def __init__(self, make, model):
        self.make = make  # Public variable
        self.model = model  # Public variable

# [Usage](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61)
my_car = Car("Toyota", "Corolla")
print(my_car.make)  # Accessing a public variable
print(my_car.model)  # Accessing a public variable

In this example, make and model are public variables that can be accessed directly from outside the Car class. They provide information about the car's make and model, illustrating how public variables form part of the class's interface.

Conclusion

Public methods and variables are essential for defining how a class interacts with the outside world. They provide a controlled way to access and modify an object's data and behavior, forming the backbone of object-oriented programming in Python. By carefully designing public interfaces, developers can create robust and maintainable code that leverages the power of encapsulation and abstraction.

For more detailed discussions, you can refer to the following resources:

| Book Title | Usage of public | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|-----------------|-----------------|-------------------------------|---------------|-----------------------|
| [Python How-To](https://www.manning.com/books/python-how-to) | Discusses public methods and attributes as accessible from outside the class. [more](https://livebook.manning.com/book/python-how-to/chapter-8/140) | Explains the default behavior of public members in Python. [more](https://livebook.manning.com/book/python-how-to/chapter-8/140) | Connects public members to object-oriented programming principles. [more](https://livebook.manning.com/book/python-how-to/chapter-8/140) | Provides a class example with public methods and attributes. [more](https://livebook.manning.com/book/python-how-to/chapter-8/140) | Highlights the accessibility and modification of public members. [more](https://livebook.manning.com/book/python-how-to/chapter-8/140) |
| [Practices of the Python Pro](https://www.manning.com/books/practices-of-the-python-pro) | Describes public methods and variables as part of a class's interface. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61) | Focuses on how public methods and variables enable interaction with a class. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61) | Emphasizes the role of public interfaces in encapsulation and abstraction. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61) | Includes examples of public methods and variables in a class. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61) | Discusses the importance of public interfaces for robust code. [more](https://livebook.manning.com/book/practices-of-the-python-pro/chapter-3/61) |