Answer:
class Book:
def __init__(self, book_title, book_author, book_publisher):
self.__book_title = book_title
self.__book_author = book_author
self.__book_publisher = book_publisher
Step-by-step explanation:
Python kind of haves private data attributes, although technically they don't.
Take for example the following code:
"
class A:
def __init__(self):
self.__privateAttribute = 0
x = A()
print(x.__privateAttribute)
"
all this really does is rename the variable name, and if you run the following code:
print(dir(x))
it will print the attributes of "x" which include "_A__privateAttribute" which is the attribute that was initialized.
Anyways getting that out of the way, you need to now about the __init__ method, which runs each time you initialize a new instance of the class. It's a way of setting up some necessary values that will be used in the objects methods (the functions inside the class)
So, because we simply cannot know what the book_title, book_author, and book_publisher are (since they vary book to book...), we take them as arguments using the __init__ method, and then initialize their values in their.
"
class Book:
def __init__(self, book_title, book_author, book_publisher):
self.__book_title = book_title
self.__book_author = book_author
self.__book_publisher = book_publisher
"
I just realized I completely forgot to even mention what the "self" variable stands for. Whenever you initialize a variable, or call a method from an instance of the class, the first argument passed will be the instance of the class. Take for example the following class
"
class A:
def __init__(self, b):
self.b = b
def printB(self):
print(self.b)
def printB(self):
print(self.b)
c = A(3)
d = A(4)
c.printB()
printB(c)
d.printB()
printB(d)
"
The two lines
c.printB() and printB(c) are the same exact thing, the only difference is when you call c.printB(), you automatically pass the "c" as the first argument, and the same thing for d.printD(), it's implicitly passed whenever you call a method from an instance of the class.