0% found this document useful (0 votes)
15 views3 pages

Encapsulation

The document consists of two worksheets focused on Python visibility conventions and encapsulation. The first worksheet involves analyzing a Book class to determine the intended visibility of its attributes and methods, while the second worksheet requires creating a Product class with specific attributes and methods, including validation for price updates. Both worksheets emphasize understanding and applying Python's conventions for attribute visibility and encapsulation principles.

Uploaded by

chitturishrika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Encapsulation

The document consists of two worksheets focused on Python visibility conventions and encapsulation. The first worksheet involves analyzing a Book class to determine the intended visibility of its attributes and methods, while the second worksheet requires creating a Product class with specific attributes and methods, including validation for price updates. Both worksheets emphasize understanding and applying Python's conventions for attribute visibility and encapsulation principles.

Uploaded by

chitturishrika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Worksheet

Name: _________________________ Date: _______________

Worksheet 1: Understanding Visibility Conventions

Instructions: Consider the following incomplete Python class definition for a Book. Indicate
the intended visibility (public, protected, private-like) of each attribute and method based
on its name. Explain your reasoning based on Python conventions.

class Book:

def __init__(self, title, author, pages):

[Link] = title

self._author = author

self.__pages = pages

def get_title(self):

return [Link]

def _get_author(self):

return self._author

def get_pages(self):

return self.__pages

def set_pages(self, new_pages):

if isinstance(new_pages, int) and new_pages > 0:

self.__pages = new_pages

else:

print("Invalid page count.")

def display_info(self):

print(f"Title: {[Link]}, Author: {self._author}, Pages: {self.__pages}")

def _internal_calculation(self):
return self.__pages * 2

def __secret_process(self):

print("Processing...")

# Create an instance of the Book class

my_book = Book("The Great Novel", "A Famous Writer", 350)

# Indicate whether the following lines of code are generally considered acceptable

# according to Python visibility conventions. Explain why or why not.

# print(my_book.title)

# print(my_book._author)

# print(my_book.__pages)

# print(my_book.get_title())

# print(my_book._get_author())

# print(my_book.get_pages())

# my_book.set_pages(400)

# my_book._internal_calculation()

# my_book.__secret_process()

Worksheet 2: Applying Encapsulation in Python

Instructions:

Create a new Python class called Product with the following attributes: _product_id
(String), _name (String), and __price (float). Use the underscore conventions to indicate
their intended visibility.

Provide getter methods for the product ID and name (get_product_id(), get_name()).

Provide a getter and setter method for the price (get_price(), set_price(new_price)). Include
a validation check in the set_price method to ensure the price is not negative. If the new
price is invalid, display an error message and do not update the price.
Create a public method display_product_info() that prints the product's ID, name, and
price.

Create an instance of the Product class in the main part of your script, set its ID and name
(using the constructor), and then demonstrate how to access the information using the
getter methods and update the price using the setter method. Show an example of trying to
set an invalid price.

You might also like