Professional Documents
Culture Documents
Unit 5 5marks
Unit 5 5marks
Unit 5 5marks
A class specifies the set of instance variables and methods that are “bundled
together” for defining a type of object. Class is a blueprint or template to define an
object. Attributes are the names given to the variables that make up a class.
A class instance with a defined set of properties is called an object. As a result, the
same class can be used to construct as many objects as needed.
class Book:
def __init__(self, title, quantity, author, price):
self.title = title
self.quantity = quantity
self.author = author
self.price = price
The __init__ special method, also known as a Constructor, is used to initialize the
Book class with attributes such as title, quantity, author, and price.
In Python, built-in classes are named in lower case, but user-defined classes are
named in Camel or Snake case, with the first letter capitalized.
This class can be instantiated to any number of objects. Three books are instantiated
in the following example code:
book1, book2 and book3 are distinct objects of the class Book. The term self in the
attributes refers to the corresponding instances (objects).
print(book1)
print(book2)
print(book3)
Output:
The class and memory location of the objects are printed when they are printed. We
can't expect them to provide specific information on the qualities, such as the title,
author name, and so on. But we can use a specific method called __repr__ to do
this.
In Python, a special method is a defined function that starts and ends with two
underscores and is invoked automatically when certain conditions are met.
class Book:
def __init__(self, title, quantity, author, price):
self.title = title
self.quantity = quantity
self.author = author
self.price = price
def __repr__(self):
return f"Book: {self.title}, Quantity: {self.quantity}, Author: {self.author}, Price:
{self.price}"
print(book1)
print(book2)
print(book3)
Output:
Class A is the superclass of all the classes in the figure. Thus, subclasses B
and E each inherit variable var1 and method method1 from Class A. In addition,
Class B defines variable var2 and method method2, and Class E defines method5.
Since Class C is a subclass of Class B, it inherits everything in Class A and Class B,
adding var3 and method3 in its own definition. And since Class D is also a subclass
of Class B, it inherits everything in Class A and Class B, also defining method4.
>>> fruit
True
Sets do not maintain a logical ordering. Therefore, it is invalid and makes no sense
to access an element of a set by index value.
The add and remove methods allow sets to be dynamically altered during program
execution, for example,
In the above example, to create an empty an empty set1, the notation set () is
used, since empty braces is used to create an empty dictionary. Because sets do not
have duplicate elements, adding an already existing item to a set results in no
change to the set.
x= set ()
print (x)
print (n)
Output:
set()
{0, 1, 2, 3, 4}
>>>
Set Types
1. set type-
The set type is mutable---the contents can be changed using methods like
add() and remove(). Since it is mutable, it has no hash value and cannot be used as
either a dictionary key or as an element of another set.
2. frozenset type: The frozenset type is immutable and hashable ---its contents
cannot be altered after it is created; however, it can be used as a dictionary key or as
an element of another set.
Example:
As shown in the above example, the values of a set of type frozenset must be
provided in a single list when defined. (A frozenset type is needed when a set is
used as a key value in a given dictionary.)
Public members of a class, on the other hand, are directly accessible. For
example, the following are valid method calls,
frac1.getNumerator() ALLOWED
frac1.getDenominator() ALLOWED
frac1.setNumerator(4) ALLOWED
frac1.setDenominator(6) ALLOWED
These methods are referred to as getters and setters since their purpose is to
get (return) and set (assign) private instance variables of a class. Restricting access
to instance variables via getter and setter methods allows the methods to control
what values are assigned (such as not allowing an assignment of 0 to the
denominator), and how they are represented when retrieved. Thus, the instance
variables of a class are generally made private, and the methods of the class
generally made public.
The class keyword is used to define classes, much as def is used for defining
functions. All lines following the class declaration line are indented. Instance
variables are initialized in the __init__ special method. Being private, instance
variables __numerator and __denominator are not meant to be directly accessed.
>>> frac1.__numerator
>>>frac1._Fraction__numerator
self.__numerator = numerator
self.reduce()
return self._numerator
def getDenominator (self):
return self._denominator
self.__numerator - value
"""
if value == 0:
To understand this, all private class members are automatically renamed to begin
with a single underscore character followed by the class name. Such renaming of
identifiers is called name mangling. Unless the variable or method is accessed with
its complete (mangled) name, it will not be found. Name mangling prevents
unintentional access of private members of a class, while still allowing access when
needed.