Posts: 8
Threads: 3
Joined: Nov 2020
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?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Nov-09-2020, 12:01 PM
(This post was last modified: Nov-09-2020, 12:01 PM by buran.)
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 ?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Nov-09-2020, 12:05 PM
(This post was last modified: Nov-09-2020, 12:05 PM by buran.)
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...
Posts: 8
Threads: 3
Joined: Nov 2020
(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) ?
Posts: 8,160
Threads: 160
Joined: Sep 2016
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 ?
Posts: 8
Threads: 3
Joined: Nov 2020
Nov-09-2020, 01:32 PM
(This post was last modified: Nov-09-2020, 01:32 PM by TomasAm.)
(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.
Posts: 8,160
Threads: 160
Joined: Sep 2016
It looks like you are somewhat confused. What do you mean to "happen" inside class?
Posts: 8
Threads: 3
Joined: Nov 2020
Nov-09-2020, 01:50 PM
(This post was last modified: Nov-09-2020, 01:50 PM by TomasAm.)
(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)
Posts: 8,160
Threads: 160
Joined: Sep 2016
Nov-09-2020, 01:57 PM
(This post was last modified: Nov-09-2020, 01:57 PM by buran.)
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.
Posts: 8
Threads: 3
Joined: Nov 2020
(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."
|