Python Forum
Pass an object to a class, then make an object of it and pass again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass an object to a class, then make an object of it and pass again
#1
Good day,

I have 4 classes as follows:
class Body:
    def __init__(self, top, bottom):
        self.top = top
        self.bottom = bottom         
class Top:
    def __init__(self, eyes, mouth, nose):
        self.eyes = eyes
        self.mouth = mouth
        self.nose = nose
class Bottom:
    def __init__(self, feets, legs):
        self.feets = feets
        self.legs = legs
class Fabric:
    def __init__(self, humans):
        self.Body = humans
With these classes so far I could do like this:
tope = Top(3, 2, 2)
bote = Bottom(3, 3)
bode = Body(tope, bote)
print(bode.bottom.feets)
Here I create objects from "Top" and "Bottom" classes, then pass them as parameters to object "bode" created from "Body" class.
So finally my question is, How would I pass this "bode" object in the forth class "Fabric".

Can I reach variable "eyes" for example inside Fabric class and modify it?
Reply
#2
fabric = Fabric(bode)
print(fabric.Body.bottom.feets)
fabric.Body.top.eyes = 2 # 2 eyes
print(fabric.Body.top.eyes)
I must comment on the inconsistencies in your class - e.g. Fabric.Body attribute, why Body and not body? Also normally the params in __init__ will be the same as the attribute you assign, i.e. why humans and not body?
TomasAm likes this post
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
Also, because this is homework, I don't know if you are familiar with the concept of inheritance and given that we don't have the full assignment - is it possible that you are asked to have some of your classes inherit from some of the other? I admit that human body is not best example to implement inheritance, but who knows...
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
#4
(Nov-09-2020, 12:05 PM)buran Wrote: Also, because this is homework, I don't know if you are familiar with the concept of inheritance and given that we don't have the full assignment - is it possible that you are asked to have some of your classes inherit from some of the other? I admit that human body is not best example to implement inheritance, but who knows...

Regarding inconsistencies, I will read and improve, I thought it is not important.

I know the inheritance, however its not specified to use it or not.

1. Is it possible to pass "bode" object further into the "Fabric" and modify "eyes" variable.
2. How to modify eyes variable which is inside "bode" or any other "Body" class object which may be created.

tope = Top(3, 2, 2)
bote = Bottom(3, 3)
bode = Body(tope, bote)
----fabric = Fabric(bode) ?
Reply
#5
did you read my post?
(Nov-09-2020, 12:01 PM)buran Wrote:
fabric = Fabric(bode)
print(fabric.Body.bottom.feets)
fabric.Body.top.eyes = 2 # 2 eyes
print(fabric.Body.top.eyes)
I must comment on the inconsistencies in your class - e.g. Fabric.Body attribute, why Body and not body? Also normally the params in __init__ will be the same as the attribute you assign, i.e. why humans and not body?
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
#6
(Nov-09-2020, 01:14 PM)buran Wrote: did you read my post?
(Nov-09-2020, 12:01 PM)buran Wrote:
fabric = Fabric(bode)
print(fabric.Body.bottom.feets)
fabric.Body.top.eyes = 2 # 2 eyes
print(fabric.Body.top.eyes)
I must comment on the inconsistencies in your class - e.g. Fabric.Body attribute, why Body and not body? Also normally the params in __init__ will be the same as the attribute you assign, i.e. why humans and not body?

Yes, I try to apply it now
fabric.Body.top.eyes = 2 # 2 eyes
so this line logic happens inside Fabric class(not like now - this line is outside class). I am trying to create a function as solution. So when data arrives to this Fabric class data gets modified.
Reply
#7
It looks like you are somewhat confused. What do you mean to "happen" inside class?
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
#8
(Nov-09-2020, 01:32 PM)TomasAm Wrote:
(Nov-09-2020, 01:14 PM)buran Wrote: did you read my post?

Yes, I try to apply it now
fabric.Body.top.eyes = 2 # 2 eyes
so this line logic happens inside Fabric class(not like now - this line is outside class). I am trying to create a function as solution. So when data arrives to this Fabric class data gets modified.

(Nov-09-2020, 01:43 PM)buran Wrote: It looks like you are somewhat confused. What do you mean to "happen" inside class?

I mean, can the logic you have written be inside Fabric class? Something like this(it is not functional, i am still trying to find out):
class Fabric:
    def __init__(self, humans):
        self.Body = humans
    def action(self)
        fabric.Body.top.eyes = 4
        print(fabric.Body.top.eyes)
maybe something like this?:
def action(self)
        humans.Body.top.eyes = 4
        print(humans.Body.top.eyes)
Reply
#9
class Fabric:
    def __init__(self, humans):
        self.Body = humans
    def set_eyes(self, value)
        self.Body.top.eyes = value
        print(self.Body.top.eyes)
then fabric.set_eyes(4).

you need to use self, not fabric. self is the instance, which is passed automatically. But this is anti-pattern in python. Normally we don't need need setters and getters just to modify a property. We will do fabric.Body.top.eyes = 4 outside the class, without the need to define Fabric.set_eyes() method.
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
#10
(Nov-09-2020, 01:57 PM)buran Wrote:
class Fabric:
    def __init__(self, humans):
        self.Body = humans
    def set_eyes(self, value)
        self.Body.top.eyes = value
        print(self.Body.top.eyes)
then fabric.set_eyes(4).

you need to use self, not fabric. self is the instance, which is passed automatically. But this is anti-pattern in python. Normally we don't need need setters and getters just to modify a property. We will do fabric.Body.top.eyes = 4 outside the class, without the need to define Fabric.set_eyes() method.

Thank you, now it makes sense! I will read more about this "anti-pattern in python."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class and Object HW for cars - Help with error handling for user inputs jakeshugart 2 298 May-09-2024, 05:00 PM
Last Post: jakeshugart
  Object Detection with ESP32 CAM. MateoG 0 645 Oct-11-2023, 01:44 PM
Last Post: MateoG
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 1,568 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  how to pass named arguments arunan 1 3,999 Jan-18-2021, 01:30 PM
Last Post: buran
  Animating 2D object movement with matplotlib esamalihalil1993 0 1,808 Nov-23-2020, 05:49 PM
Last Post: esamalihalil1993
  AttributeError: 'str' object has no attribute 'size' russoj5 4 7,555 Nov-15-2020, 11:43 PM
Last Post: deanhystad
  Task that i can't pass c06a8acb 6 2,656 Nov-11-2020, 07:50 AM
Last Post: c06a8acb
  Object has no attribute 'replaceall' ? peterp 2 7,271 Nov-10-2020, 09:23 PM
Last Post: buran
  Help on stimulating approaching an object javesike1262 9 3,816 Oct-23-2020, 01:08 AM
Last Post: javesike1262
  Returning data from a pyobjc object to python environment newpythonuser100 3 2,499 Jul-28-2020, 12:08 PM
Last Post: newpythonuser100

Forum Jump:

User Panel Messages

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