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
#1
Hello,

I am pretty new to python and OOP, so please don't be too harsh on my question Smile
I have the following code:

class frame:
    def __init__(self, width, height):
        self.frame_width = width
        self.frame_height = height

    def image(self):
         self.width = 0.8 * self.frame_width
         self.height = 0.8 * self.frame_height

picture = frame(60,40)
My question is, how can I access the image width in the following manner:
picture.image.width
Reply
#2
I will start with more general remarks. It is convention that class name should start with capital letter, so better use Frame, not frame. Then it looks like you actually want image to be a class on its own (i.e. the image inside the frame), right? At the moment it's a method of class Frame. If it would not be a class on its own, then width and height would be properties of the frame And probably you want them to be fixed as proportion to frame size, so use @property decorator

class Frame:
    def __init__(self, width, height):
        self.frame_width = width
        self.frame_height = height
        
    @property
    def image_width(self):
        return 0.8 * self.frame_width

    @property
    def image_height(self):
        return 0.8 * self.frame_height


picture = Frame(100, 200)
print(f'Frame: {picture.frame_width}X{picture.frame_height}, picture:{picture.image_width}X{picture.image_height}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-11-2025, 07:29 AM)buran Wrote: I will start with more general remarks. It is convention that class name should start with capital letter, so better use Frame, not frame. Then it looks like you actually want image to be a class on its own (i.e. the image inside the frame), right? At the moment it's a method of class Frame. If it would not be a class on its own, then width and height would be properties of the frame And probably you want them to be fixed as proportion to frame size, so use @property decorator

class Frame:
    def __init__(self, width, height):
        self.frame_width = width
        self.frame_height = height
        
    @property
    def image_width(self):
        return 0.8 * self.frame_width

    @property
    def image_height(self):
        return 0.8 * self.frame_height


picture = Frame(100, 200)
print(f'Frame: {picture.frame_width}X{picture.frame_height}, picture:{picture.image_width}X{picture.image_height}')

Thank you so much for your reply and solution! I think I figured out how to achieve what I was originally going for. If you want to have an image with a property 'width' and then a frame with the same property, you need to have two different classes with the same property. If your image size depends somehow on the frame size, and you want to have a property image in your frame class, one can initialize the image class within the frame class as follows:

class frame:
    
    def __init__(self, width, height):
        p = 0.8
        self.width = width
        self.height = height
        self.image = image(self.width, self.height, p) 
 
class image:
    def __init__(self, width, height, p):     
        self.width  = p * width
        self.height = p * height

 
picture = frame(60,40)
print(picture.image.width)
48.0
print(picture.width)
60
Reply
#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
#5
(Apr-11-2025, 07:41 PM)deanhystad Wrote: 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.


Thank you for your help. I appreciate that. I like the solution and this was what I was looking for. I will implement that into my code.
Reply
#6
If you return something from image() then you have it.

class Frame:
    
    def __init__(self, width, height, factor):
        self.frame_width = width
        self.frame_height = height
        self.factor = factor
 
    def image(self):         
         return (self.frame_width * self.factor, self.frame_height * self.factor)

Picture = Frame(60,40,0.8)
tup = Picture.image() # returns (48.0, 32.0)
Reply
#7
(Apr-12-2025, 12:59 AM)Abedin Wrote: I like the solution and this was what I was looking for. I will implement that into my code.
Just keep in mind, that Image.width and Image.height can be manipulated separately and thus can break the proportionality factor you set for Frame
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 1,419 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,347 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,837 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using one child class method in another child class garynewport 5 3,157 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Accessing same python script from multiple computers bigrockcrasher 1 2,897 May-25-2022, 08:35 PM
Last Post: Gribouillis
  Python modules for accessing the configuration of relevant paths Imago 1 2,383 May-07-2022, 07:28 PM
Last Post: Larz60+
  accessing value in dict_values class CompleteNewb 14 8,641 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,986 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Distinguishing different types of class attributes Drone4four 4 3,854 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