Class

Ojects and Instances

1. Explain object intuitively (What does it contain?).
A object is a container / entity that contains data (State and attributes) and functionality (behaviours or actions).
2. What is an object in Python? Provide some examples.
In Python, objects are instances of classes. Data types such as integers, strings or arrays are all objects in Python.
3. An object possesses state, behavior, and identity. Explain the three components.
  • State represents the attributes or properties that describe the object.
    • Attributes are variables or parameters that belongs to an instance of a class
  • Behaviour of an object is defined by the methods or functions associated with the object.
    • Behaviours identifies the actions the object can perform with its data.
  • Identity of an object is a unique name (identifier) to an object such that we can call on them to interact with other objects.
4. Provide an example of an object that represents an employee. Include the state, behaviour and the identity.
  • Identity Name of the employee (George)
  • Attributes(State): Age, income, home address
  • Behaviours: Working, Paying Taxes, Traveling
5. Explain the term instances (Hint: Where are they created from?).
Objects created from classes are instances of that class
6. Explain the term instantiation.
Creating an object through the class function is called instantiation.
7. Explain the key difference between a class and an instance. Provide an intuitive example.

A class is a blueprint for creating the objects. Whereas an instance is a specific individual occurrence of a class.

Example: A class is questionnaire print, instance is the questionnaire form that has been filled out with information.

8. Provide the 2 methods to determine the class from which the object was instantiated.
  • type(<instance>)
  • instance.__class__
9. Explain class body scope.
Class body scope is where class-level attributes, methods, and other class-related elements are defined.

Attributes and Methods

1. Is a class instance’s namespace distinct from the class itself?
Yes, a class instance’s namespace is distinct from the class.
2. Provide the two ways to retrieve attribute values.
  • Myclass.att
  • getattr(Myclass, att)
3. Provide the two ways to modify and add attributes.
  • class.attribute_name = attribute_value
  • setattr(class , attribute_name, attribute_value)
4. How to delete a class instance attributes?
delattr(class, attribute_name)
5. Explain class methods.
Methods are functions defined within a class. They operate on the data stored in the instance and can perform various actions.
6. Explain the difference between class data attributes and instance data attributes. When should one be used over the other?
  • Instance attributes are attributes that belong to that individual instances, that can vary in values.
    • Use it to define properties that varies from one instance to another.
  • Class attributes is a property that remains the same for all class instances.
    • Use it to define properties that share the same value for every class instances.
7. Is it possible to access class data attributes if it’s not specified explicitly in the instance’s attributes?
Yes, if Python cannot find the attribute in the instance’s namespace, it will look at the type(class) attributes.
8. How to override the class data attribute?

Modify the class attribute directly by referencing the class itself.

class MyClass:
    class_attribute = "I am a class attribute"

# Access class attribute through the class
print(MyClass.class_attribute)  # Output: I am a class attribute

# Override the class attribute
MyClass.class_attribute = "I am the modified class attribute"

# Access class attribute through the class and instances
print(MyClass.class_attribute)  # Output: I am the modified class attribute
9. Explain the differences between methods and functions.
A method behaves like a function. However, the key difference is that the function is a standalone block of code that can be defined at the module (global) level. In contrast, methods is a function that is bound by another object and it is defined within a class and operates on the data(attributes of that class).
10. Explain the process of creating an instance in the context of __init__(self).
  • The __init__ method is known as the constructor and is automatically called when an object is created from a class to initialise the attributes of the instance.
  • The self parameter refers to the instance being created and it is assigns the attributes and methods to the specific instance.
11. Why is it important to include the extra argument self in the __init__() special method?
It is important to include the extra argument self because it essentially binds the methods and the attribute values to the instance’s state and behaviours. Without the self we cannot access the instance’s data.
12. Explain the instance attributes: __self__.
  • __self__: The instance the method is bound to.

Properties

1. Explain bare attributes.
Bare attributes are attributes that can be directly accessed.
2. Explain the purpose of implementing properties.
Properties are a way to control the access, modification, and deletion of attributes in a class, providing a more controlled and flexible approach than directly exposing class attributes.
3. Explain how to develop read-only properties.
Create a property with only the get property method defined.
4. Provide the syntax to delete properties using the property decorator.
@prop_name.deleter

special_methods

1. Explain __str__ main use-cases and how to call it.

Main Use Case: The primary purpose of the __str__ method is to provide a human-readable string representation of an object. It is commonly used for display purposes to end-users or for logging.

To call it: str(obj) and print(obj).

2. Explain __repr__ general use-cases and how to call it.

Main Use Case: The primary purpose of the __repr__ method is to provide an unambiguous and detailed string representation of an object. It is commonly used to describe the ways to instantiate the object.

To call it: repr(obj)

Last updated on 21 Nov 2023