Python Classes & Inheritance: Advanced Functionality Using Python Classes

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider this class as defined below: class BankAccount: def __init__(self, balance): self.__amount = balance def __mul__(self, other): if type(other) == int: return self.__amount * other else: raise ValueError('Can only multiply by int') ba = BankAccount(1000) What will be the result of this operation below? ba * 4

4000

If you want to decorate a property called "salary" as a setter what decorator would you use?

@salary.setter

When a base class inherits from ABC and when methods with no implementations are tagged with @abstractmethod, how does that help us work with base and derived classes?

Does not allow instantiation of classes where methods have no implementations

Which of the following is true about static methods in Python?

Has no reference to the state of the class

Question : Consider an object of the class Employee stored in the variable e. When you invoke the print(e) function, what does Python do under the hood?

Invoke the __str__() method and if that is not present, invoke the __repr__() method

Match the special functions with the correct arithmetic operation

Multiplying objects (__mul__()) String representation of object (__str__()) Initialization of object __init__() Adding objects __add__() Raising to a power __pow__()

You have a class named BankAccount and you have implemented the __add__() special method within the class. However, by mistake the code within this special method performs the subtraction operation on account balances rather than addition. If you have two instances of this class ba1 and ba2 what will happen when the following bit of code is executed? ba1 + ba2

The account balances are subtracted

Which of the following is true about class methods in Python?

The first argument is a reference to the class Are bound to the class and not objects of a class

Question : If you want your objects to support the + operator, what special method do you need to implement in the object's class?

__add__()

If you want the built-in len() function to work with your custom class objects what special method would you implement?

__len__()

In order to have your custom class objects support iteration using for-loops which of the following two special methods do you need to implement?

__next__() __iter__()

Which of the following are the functions that you pass in to create a property on a class?

deleter setter getter


Set pelajaran terkait

Preguntas sobre tiempo libre (10)

View Set

Chapter 15 - Accounts Receivable & Uncollectible Accounts

View Set

Chapter 23: Management of Patients With Chest and Lower Respiratory Tract Disorders

View Set