Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use python's builtin abstract base classes #100

Closed
nikochiko opened this issue Feb 25, 2020 · 4 comments
Closed

Use python's builtin abstract base classes #100

nikochiko opened this issue Feb 25, 2020 · 4 comments
Labels

Comments

@nikochiko
Copy link

Description of the problem

The current code creates a new abstract class, as in the example below. Python has dedicated abc.ABCs for abstract base classes. It would provide us more flexibility and conform better to python's standards.

Example of the problem

https://github.com/codezonediitj/pydatastructs/blob/master/pydatastructs/linear_data_structures/arrays.py

References/Other comments

https://docs.python.org/3/library/abc.html
https://www.python.org/dev/peps/pep-3119/

@nikochiko nikochiko changed the title Use in-built abstract base classes Use python's builtin abstract base classes Feb 25, 2020
@czgdp1807
Copy link
Member

What will happen to if we will do this? Will there be any enhancement in code readability or software design?
Any comments @codezonediitj/gssoc2020 @codezonediitj/maintainers

@nikochiko
Copy link
Author

nikochiko commented Feb 25, 2020

Yes,
for example, arrays are Sequences, with the corresponding ABCs, we can easily check that the given array is a Sequence with

isinstance(obj, Sequence)

More importantly, the ABCs have some abstract methods. These abstract methods need to be present in the subclasses. For example, an Iterator ABC would have the abstract methods __iter__ and __getitem__. It provides a very complete basic design for inheriting subclass.
Also as this is a python standard library, we can expect it to become the standard way of creating ABCs. Additionally any changes in the rest of python's implemention would be reflected in its implementation and we won't have the overhead to do that changes manually on our end.
So all in all, if we are going to work with ABCs, I feel using the built-in procedure would be a robust way to go on.
What are your thoughts?

@czgdp1807
Copy link
Member

So, it looks like that if you inherit an ABC then it will not allow you to instantiate a class object unless you override all the abstract methods of the base class which is kind of good but depends on the use case. If you never want to define, __iter__ in the arrays as arrays are usually meant for traversing via indexing then inheriting a class with abstract method __iter__ will be of no use.
IMO, it would make writing code more difficult as we will be forced to implement some methods which we don't want to. Having template designs is good but then customising them for our needs becomes difficult. It's good in those places where a base class provides an interface which is completely used by all the subclasses. In our case there are some DS which don't inherit all the methods completely from the base classes.
Let's see what others have to say.

@czgdp1807
Copy link
Member

It's just about making things complex and difficult to maintain. Closing for now. You can go to #254 and #255 for doing these kind of things. Thanks for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants