Python Forum
I there a more efficient way of printing ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I there a more efficient way of printing ?
#7
Complete different approach, not only about printing/representation. Requires Python 3.7+
The dataclass decorator makes a nice representation from a class and some other convenient stuff. In addition, usually you want to keep points together in a tuple, a sequence or mapping. If you have more than one point, it's better to keep x, y and z together in one object.

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

print(point1)
print(point2)
If you want to sort Points by x, y and z, dataclass needs the extra argument order. The Argument frozen will prevent assigning new objects to the Point object. Then the dataclass behaves like a tuple.


from dataclasses import dataclass

@dataclass(order=True, frozen=True)
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

sorted_points = sorted([point1, point2])
print(sorted_points)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: I there a more efficient way of printing ? - by DeaD_EyE - Dec-01-2020, 10:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 523 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,519 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,970 Oct-06-2022, 07:47 AM
Last Post: DPaul
  A more efficient way of comparing two images in a python vukan 0 2,052 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to make iterative search more efficient renergy 2 2,297 Jan-03-2020, 03:43 PM
Last Post: stullis
  Simple problem. looking for an efficient way silverchicken24 3 2,388 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Most efficient way to define sub keys of a dictionary? wrybread 1 2,148 Feb-21-2019, 12:23 AM
Last Post: snippsat
  Efficient way of iterating a list of records anguis 4 3,107 Feb-19-2019, 03:39 AM
Last Post: scidam
  Is there a more efficient way to code this project? gkiranseo 1 2,718 Sep-25-2018, 09:51 AM
Last Post: Larz60+
  Computationally efficient recording + searching? IAMK 1 2,396 Apr-30-2018, 01:23 AM
Last Post: IAMK

Forum Jump:

User Panel Messages

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