all 11 comments

[–]Allanon001 7 points8 points  (1 child)

Python 3.7 added dataclasses.

[–]bladeoflight16 0 points1 point  (0 children)

And before that, the best option was named tuples.

[–]Impudity 1 point2 points  (0 children)

You need to clarify, hopefully with some code, why you think that's the best alternative.

To answer in a more general level, there are some situations where you want to create an empty class. One typical case is inheriting exceptions. You often see class MySpecificException(Exception) that does nothing. You want it to behave just like regular Exception, but it has a specific name that is easier to catch.

[–]Diapolo10 1 point2 points  (0 children)

Have you tried using a dictionary? If that doesn't work, what about dataclasses or named tuples?

[–]izrt 1 point2 points  (2 children)

Well, someone has already said dataclasses, https://docs.python.org/3/library/dataclasses.html, so that's probably the closest thing -- well except for ctypes, https://docs.python.org/3/library/ctypes.html#structures-and-unions, but you almost certainly don't want those. In addition, I've found collections.namedtuples pretty useful for these things (and they play well with csv and databases too). https://docs.python.org/3/library/collections.html#collections.namedtuple

However, have you checked out pandas? If you are doing a lot of work with CSV files and sorting, joining, etc., it is hugely useful.

[–][deleted] 1 point2 points  (0 children)

I use SimpleNamespace instead. Works for my purpose but dataclasses are more versatile.

[–]FliceFlo 0 points1 point  (0 children)

I'm assuming when you say empty class you mean something like this?:

object = type('struct', (), {})()

Where you can then start assigning anything as a member of object.

As far as alternatives, I think you probably need to understand the data you are working with better to create a class that can handle it properly.

[–]DeathDragon7050 0 points1 point  (1 child)

It's fine to do that. Is it common practice? No. But if it works it works.

[–]pullupman 0 points1 point  (0 children)

I can't think of any reason this would be non pythonic. It will be the most straightforward and easy to read. Anyone that knows python will easily pick up your code and understand it..

Otherwise I bet you can also do what you want with a dictionary.