Python Forum
Accessing method attributes of python class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing method attributes of python class
#4
I think it makes a lot more sense to do this:
class Frame:
     
    def __init__(self, width, height):
        p = 0.8
        self.width = width
        self.height = height
        self.image = Image(self.width * p, self.height * p) 
  
class Image:
    def __init__(self, width, height):     
        self.width  = width
        self.height = height
Having width as an argument to Image.__init__ and as an attribute, it is weird that the two values would not match immediately after an instance is created.

Now is a good time to start following python coding conventions. Class names start with an uppercase letter. This makes it easy to tell the difference between creatin an instance of a class and calling a function.

p is an odd thing. There are multiple ways that p can be handled. You could make p a class attribute.
class ImageFrame:

    PROPORTIONALITY = 0.8

    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.image = Image(self.width * self.PROPORTIONALITY, self.height * self.PROPORTIONALITY) 
An advantage with this is code can ask the ImageFrame (a better name than Frame for a Frame that's defining attribute is it contains an image) what is the value of the proportionality constant. It also makes the proportionality constant stand out. If you later decide that the images should be 90% as big as the frame, it is easy to find where proportionality is set.

You could make p an argument in the init method. If you want p to be 0.8 most of the time, you can set a default value.
class ImageFrame:

    def __init__(self, width, height, proportionality=0.8):
        self.width = width
        self.height = height
        self.image = Image(self.width * proportionality, self.height *proportionality) 
An advantage to this approach is you can make different image frames with different p values.
Abedin likes this post
Reply


Messages In This Thread
RE: Accessing method attributes of python class - by deanhystad - Apr-11-2025, 07:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 1,422 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,349 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,839 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using one child class method in another child class garynewport 5 3,158 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Accessing same python script from multiple computers bigrockcrasher 1 2,900 May-25-2022, 08:35 PM
Last Post: Gribouillis
  Python modules for accessing the configuration of relevant paths Imago 1 2,385 May-07-2022, 07:28 PM
Last Post: Larz60+
  accessing value in dict_values class CompleteNewb 14 8,643 Mar-31-2022, 04:02 AM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,890 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 3,996 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Distinguishing different types of class attributes Drone4four 4 3,864 Feb-21-2022, 06:34 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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