Python Forum
Some help with my first python class and importing ....im making a lidar program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some help with my first python class and importing ....im making a lidar program
#4
Why are you doing this?
        self.__angle_min = 0.0 
A single leading underscore is the convention for marking an attribute for internal use only (pseudo private). Leading with a doulbe underscore should be saved for the special case where subclasses of a class should not have access to the attribute. self.__angle_min = 0 creates an attribute with a mangled name _LaserProjection__angle_min. In the LaserProjection class, self.__angle_min will return this variable, but it will generate an error in a subclass.
class LaserProjection:
    def __init__(self):
        self.__angle_min = 0

    def __str__(self):
        return f"Angles = {self.__angle_min}"


class LaserProjectionSubclass(LaserProjection):
    def __init__(self):
        self.__angle_max = 0

    def __str__(self):
        return f"Angles = {self.__angle_min}, {self.__angle_max}"


print(LaserProjection())
print(LaserProjectionSubclass())
Error:
Angles = 0 Traceback (most recent call last): File "...test.py", line 18, in <module> print(LaserProjectionSubclass()) File "...test.py", line 14, in __str__ return f"Angles = {self.__angle_min}, {self.__angle_max}" AttributeError: 'LaserProjectionSubclass' object has no attribute '_LaserProjectionSubclass__angle_min'. Did you mean: '_LaserProjectionSubclass__angle_max'?
Why do you do this?
import laser_geometry
import laser_geometry.laser_geometry as lg
If you only use laser_geometry.laser_geometry, you do not need to also import laser_geometry.

Some vertical whitespace will make your program much easier to read. Two blank lines before a class or a function is suggested.
jttolleson likes this post
Reply


Messages In This Thread
RE: Some help with my first python class and importing ....im making a lidar program - by deanhystad - Jul-26-2023, 08:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Lidar in python - Quaternions, Angular Velocity, Linear Accelleration? jttolleson 2 667 Nov-27-2023, 02:05 AM
Last Post: jttolleson
  python standard way of importing library mg24 1 1,021 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,359 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  making variables in my columns and rows in python kronhamilton 2 1,749 Oct-31-2021, 10:38 AM
Last Post: snippsat
Smile Help making number analysis program Dainer 2 1,880 Jun-24-2021, 09:55 PM
Last Post: jefsummers
  Importing issues with base class for inheritance riccardoob 5 4,982 May-19-2021, 05:18 PM
Last Post: snippsat
  newbie question....importing a created class ridgerunnersjw 5 2,848 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Importing Program Wide JarredAwesome 4 2,331 Sep-07-2020, 04:34 PM
Last Post: JarredAwesome
  importing a list of numbers into python script barrypyth 8 4,903 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  Importing python data to Textfile or CSV yanDvator 0 1,867 Aug-02-2020, 06:58 AM
Last Post: yanDvator

Forum Jump:

User Panel Messages

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