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
#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


Messages In This Thread
RE: Pass an object to a class, then make an object of it and pass again - by TomasAm - Nov-09-2020, 02:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing an object from a graphic window guziakj 1 688 Jan-19-2025, 10:52 PM
Last Post: deanhystad
  Class and Object HW for cars - Help with error handling for user inputs jakeshugart 2 1,506 May-09-2024, 05:00 PM
Last Post: jakeshugart
  Object Detection with ESP32 CAM. MateoG 0 1,303 Oct-11-2023, 01:44 PM
Last Post: MateoG
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 2,308 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  how to pass named arguments arunan 1 4,801 Jan-18-2021, 01:30 PM
Last Post: buran
  Animating 2D object movement with matplotlib esamalihalil1993 0 2,517 Nov-23-2020, 05:49 PM
Last Post: esamalihalil1993
  AttributeError: 'str' object has no attribute 'size' russoj5 4 9,472 Nov-15-2020, 11:43 PM
Last Post: deanhystad
  Task that i can't pass c06a8acb 6 3,754 Nov-11-2020, 07:50 AM
Last Post: c06a8acb
  Object has no attribute 'replaceall' ? peterp 2 8,588 Nov-10-2020, 09:23 PM
Last Post: buran
  Help on stimulating approaching an object javesike1262 9 5,297 Oct-23-2020, 01:08 AM
Last Post: javesike1262

Forum Jump:

User Panel Messages

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