self & class diagram !

coderatul

Atul Kushwaha

Posted on August 13, 2023

self & class diagram !

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 that self 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)}") 
Enter fullscreen mode Exit fullscreen mode
# Output
ID of self: 140697106020432
ID of a: 140697106020432
Enter fullscreen mode Exit fullscreen mode
  • As Python is an object-oriented programming language, self is an object too. Above, we are printing the id (memory location) of self 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)}")
Enter fullscreen mode Exit fullscreen mode
ID of self: 140095874908016
ID of a: 140095874908016
ID of self: 140095874907488
ID of b: 140095874907488
Enter fullscreen mode Exit fullscreen mode
  • 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 a customer (user).
  • Every item that the customer wishes to buy can be thought of as an (object), having its respective price as an (attribute).
  • Now, imagine that you are just using the word "product"(self) to refer to every item 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:

classdig

  • 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

  1. Class: The central element of the diagram represents a blueprint for creating objects. It consists of the class name, attributes, and methods.

  2. Attributes: These are the characteristics or properties of a class, represented as variables within the class. Attributes describe the state of an object.

  3. Methods: Methods define the behavior of a class. They are the functions or operations that can be performed on the objects of the class.

  4. 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.

  5. 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).

  6. Dependency: A dependency between classes signifies that one class uses another class, often through method parameters or return types.

  7. 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

  1. Visualization: Class diagrams provide a visual representation of a system's structure, making it easier to grasp complex relationships.

  2. Communication: They serve as a common language for developers, designers, and stakeholders to discuss and refine the system's design.

  3. Design Validation: Class diagrams help in early detection of design flaws and inconsistencies, leading to better software architecture.

  4. Code Generation: Many modern integrated development environments (IDEs) can generate code skeletons from class diagrams, speeding up the development process.

💖 💪 🙅 🚩
coderatul
Atul Kushwaha

Posted on August 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

self & class diagram !
oop self & class diagram !

August 13, 2023