Self ๐
- Statistically, many people think the concept of 'self' is a pain in the ass, but don't worry, I'll make it awfully simple and easy for you guys.
- First of all, you should know that
self
isn't a keyword; it's just a naming convention. You may use anything as per your convenience. - If you just take a glance at a class, you'd notice that
self
is everywhere: as a parameter in every method, while initiating attributes under the constructor. This shows thatself
plays a major role in OOP, which is absolutely true as well.
Self the OG!
- Before starting, let me tell you a golden rule of OOP: Only an object of a class can access its attributes and methods.
- Even if two methods belong to the same class, they can't call each other (as stated above, only an object has access to attributes and methods).
- Now, here is where our
self
comes in; it helps attributes and methods to communicate with each other. Although this isn't that interesting, how it does it is worth understanding. - To understand how
self
works, let's see this piece of code:
class Name:
def __init__(self, name):
self.name = name
print(f"ID of self: {id(self)}")
a = Name("atul")
print(f"ID of a: {id(a)}")
# Output
ID of self: 140697106020432
ID of a: 140697106020432
- As Python is an object-oriented programming language,
self
is an object too. Above, we are printing the id (memory location) ofself
and the id of the object "a" (the reference variable "a" which is pointing towards the object). - And we are getting the same id, but how can object and self have same addresses ?
- this is how checkout below code block
class Name:
def __init__(self, name):
self.name = name
print(f"ID of self: {id(self)}")
a = Name("atul")
print(f"ID of a: {id(a)}")
b = Name("dolphin")
print(f"ID of b: {id(b)}")
ID of self: 140095874908016
ID of a: 140095874908016
ID of self: 140095874907488
ID of b: 140095874907488
- As we can see, the id of
self
and the object "a" are the same. - And the id of
self
, id of object "b" are same as well. - Hence, according to the above observations, we can conclude that
self
refers to the current object. - I believe now you have an idea of what
self
is, what it does, and how it does it. and if still things aren't clear read conclusion it's an analogy.
Conclusion
- Let's summarize everything with an analogy:
Imagine you are a
cashier
(class), checking out products purchased by acustomer
(user). - Every
item
that the customer wishes to buy can be thought of as an (object), having its respectiveprice
as an (attribute). - Now, imagine that you are just using the word "product"(
self
) to refer to everyitem
customer wants to buy. - If the item is Doritos(object), having 2$ price(attribute) instead of saying "Doritos"(name of object) you say "product"(self). Does it make any changes to Doritos? Nothing!
-
Self
is just a clever and easy way of managing and accessing objects - imagine if concept of OOP or self wasn't there you'd have to keep track of every thing and that my friend would have made you regret more on the decision of taking computer science as a career ๐
What is a Class Diagram?
In object-oriented programming (OOP), classes are the building blocks of a software system. They encapsulate data and behavior into a single unit, allowing for modular and organized code. A class diagram is a graphical representation that showcases the structure of classes within a system and their associations. It serves as a blueprint for understanding the relationships between different classes, enabling developers to visualize the overall architecture and design decisions.
Example Class Diagram: very basic class diagram
Here's how the class diagram might look:
- At the top we have the name of the class
- Then we have attributes, attributes with a + symbol ahead of them indicates that they are public attributes (attribute 3 in public)(available for users to access)
- Attributes having a - symbol ahead pf them indicate that they are private(attributes 1 & 2 are private) (although there is nothing truly private in python, we'll look into this in further blogs )
- Then we have methods and same goes for methods + symbol indicates public methods and - Symbol indicates private methods
later in this series we'll explore class diagram of aggregation, inheritance(with it's type)
Key Elements of a Class Diagram
Class: The central element of the diagram represents a blueprint for creating objects. It consists of the class name, attributes, and methods.
Attributes: These are the characteristics or properties of a class, represented as variables within the class. Attributes describe the state of an object.
Methods: Methods define the behavior of a class. They are the functions or operations that can be performed on the objects of the class.
Association: This represents a relationship between two classes. It indicates that objects of one class are related to objects of another class in some way.
Inheritance: It depicts the "is-a" relationship between classes. One class (subclass or derived class) inherits attributes and methods from another class (superclass or base class).
Dependency: A dependency between classes signifies that one class uses another class, often through method parameters or return types.
Multiplicity: This indicates the number of instances that participate in a relationship between classes. It can be one-to-one, one-to-many, or many-to-many.
Benefits of Class Diagrams
Visualization: Class diagrams provide a visual representation of a system's structure, making it easier to grasp complex relationships.
Communication: They serve as a common language for developers, designers, and stakeholders to discuss and refine the system's design.
Design Validation: Class diagrams help in early detection of design flaws and inconsistencies, leading to better software architecture.
Code Generation: Many modern integrated development environments (IDEs) can generate code skeletons from class diagrams, speeding up the development process.
Top comments (0)