Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Abstract classes
#1
Can you make classes abstract in python?
Reply
#2
It is possible. I would have searched before asking. As you have a Java background, my advice is not to imitate Java when you write python classes, especially in the beginning. Forget about interfaces and the like, use abstract classes when you'll have more experience with Python classes, otherwise they will be more of an annoyance than a help. But it is only my advice.
Reply
#3
(Aug-29-2020, 10:57 AM)Gribouillis Wrote: , my advice is not to imitate Java when you write python classes, especially in the beginning.

Could you elaborate on this please?
Reply
#4
Well Java classes are full of Java-specific turns of code. For example Java uses getters and setters which python most of the time does not, Java code is full of Interfaces that play a crucial role in designing functions prototypes. Python is informal. The fact that a class implements such and such methods is more of a gentleman's agreement between the code using these methods and the code that sends objects of that class. Instead of an abstract class, one would simply declare an informal root class. There is probably much more to say about this but my Java is somewhat far in the past. The general idea is that python classes are much more flexible. I don't mean they are better but they belong to a different ecosystem.
Reply
#5
Yes, when learning a language, you should try to get into its mindset rather than just trying to replicate what you'd do in another.
Reply
#6
(Aug-29-2020, 04:20 PM)Gribouillis Wrote: Well Java classes are full of Java-specific turns of code. For example Java uses getters and setters which python most of the time does not, Java code is full of Interfaces that play a crucial role in designing functions prototypes. Python is informal. The fact that a class implements such and such methods is more of a gentleman's agreement between the code using these methods and the code that sends objects of that class. Instead of an abstract class, one would simply declare an informal root class. There is probably much more to say about this but my Java is somewhat far in the past. The general idea is that python classes are much more flexible. I don't mean they are better but they belong to a different ecosystem.

I think I get what you mean, thanks for that.
Reply
#7
If want a deep dive into abstract base classes in Python.
Build powerful, new data structures with Python's abstract base classes

Can also mention this bye Raymond which is good intro to OOP in Python.
Python's Class Development Toolkit
Reply
#8
(Aug-29-2020, 06:32 PM)snippsat Wrote: Can also mention this bye Raymond which is good intro to OOP in Python.
Python's Class Development Toolkit

That second video is great thanks, so essentially python developers don't bother with encapsulation?
Reply
#9
StephMon Wrote:so essentially python developers don't bother with encapsulation?
No, no, we do care about encapsulation only it is not hard-coded in the language. It is a set of good practices. This is where your intuition from other languages can help you. You know intuitively which data can be exposed and which data is encapsulated. It is not very difficult to handle.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Behavior of Abstract Base Class dgrunwal 4 2,135 Oct-15-2020, 07:19 PM
Last Post: Gribouillis
  How to convert TransactSQL into Abstract Syntax Tree (AST) using Python? bajrangbs 2 2,280 Jan-30-2020, 03:46 PM
Last Post: micseydel
  Polymorphism not working with a call to a abstract method colt 3 2,277 Nov-04-2019, 11:04 PM
Last Post: colt
  Using classes? Can I just use classes to structure code? muteboy 5 4,982 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020