Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

oop
constructor
python
communitycreator

What is the conventional vs data library in Python?

Tariq Aman

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Conventional constructor

In Python, the way to initialize a constructor is as follows:

class Educative:
def __init__(self):
# body of constructor
print("Default constructor")

This is a practice we have all been doing since the dawn of Python itself. For example, take the repr() function, that is, the representation function:

class Educative:
def __init__(self):
# body of constructor
def __repr__(self):
# body of representor

In the above code, we use these basic constructor-creating steps.

Decorator constructor

The @dataclass function of the dataclasses library provides our class, Educative, with an automatic constructor, which looks something like the following:

def __init__(self, name: str, no_of_lessons: int):
    self.name = name
    self.no_of_lessons = no_of_lessons

One more thing before we move forward. Let’s talk about the @dataclass decorator function. First, if we want the functionality, remember to use it above every class’s name. Secondly, @dataclass itself looks like this:

@dataclasses.dataclass(*, init=True, repr=True,
 eq=True, order=False, unsafe_hash=False, frozen=False,
  match_args=True, kw_only=False, slots=False)

We can also specify and customize this decorator function:

@dataclass(init=True, repr=True, eq=False, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=True, slots=True)
class Educative:

Conventional approach

  • Easy to use but not layered.
  • Less resilient to industry standards and general practice.

Data classes

  • Structured, layered, but not beginner-friendly.
  • Resilient to most industry standards and is in use as a general practice.
class Educative:
def __init__(self, name, no_of_lessons):
self.name = name
self.no_of_lessons = no_of_lessons
obj_educative = Educative("koko", 55)
print("Name:", obj_educative.name)
print("No of lessons:", obj_educative.no_of_lessons)

Conclusion

In conclusion, the conventional method has its issues, and to overcome these issues, we use decorator functions such as @dataclass.

RELATED TAGS

oop
constructor
python
communitycreator

CONTRIBUTOR

Tariq Aman
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring