Class
Ojects and Instances
1. Explain object intuitively (What does it contain?).
2. What is an object in Python? Provide some examples.
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?).
6. Explain the term 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.
Attributes and Methods
1. Is a class instance’s namespace distinct from the class itself?
2. Provide the two ways to retrieve attribute values.
Myclass.attgetattr(Myclass, att)
3. Provide the two ways to modify and add attributes.
class.attribute_name = attribute_valuesetattr(class , attribute_name, attribute_value)
4. How to delete a class instance attributes?
delattr(class, attribute_name)
5. Explain class methods.
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?
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.
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
selfparameter 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?
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.
2. Explain the purpose of implementing properties.
3. Explain how to develop read-only properties.
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)