Python Forum
Object Oriented programming (OOP) problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object Oriented programming (OOP) problem
#7
(May-31-2021, 03:53 PM)deanhystad Wrote: This starts with a blank class and adds attributes.
class Blank():
    pass

class NotBlank():
    def __init__(self):
        self.a = 1
        self.b = 2.0
        self.c = 'three'

def only_these_types(types, source_object):
    blank = Blank()
    for name, value in source_object.__dict__.items():
        if type(value) in types:
            setattr(blank, name, value)
    return blank


for name, value in only_these_types((int, str), NotBlank()).__dict__.items():
    print(name, value)
Something like this would only work for a "data class". The new object has none of the method attributes of the source class.

Thanks
Reply


Messages In This Thread
RE: Object Oriented programming (OOP) problem - by OmegaRed94 - May-31-2021, 07:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can someone help me solve this programming problem? SuchUmami 6 962 Nov-20-2023, 10:01 AM
Last Post: EdwardMatthew
Question Linear Programming Problem Axel_LF 0 755 Feb-23-2023, 11:03 PM
Last Post: Axel_LF
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,995 Mar-21-2021, 05:17 PM
Last Post: yome
  Basic programming problem darek88 1 2,031 Sep-30-2019, 01:13 PM
Last Post: ichabod801
  Please help! Problem with my Point object itrema 2 5,284 Mar-05-2019, 09:57 AM
Last Post: itrema
  Object Oriented Programming jackbk 8 4,507 Aug-24-2018, 01:13 PM
Last Post: Windspar
  Object Oriented DB Interactions datasundae 2 2,406 May-25-2018, 09:51 PM
Last Post: datasundae
  Oriented Object ldthan 1 2,424 Mar-27-2018, 11:26 AM
Last Post: Larz60+
  problem with "hiding" object league55 4 3,234 Jan-16-2018, 11:21 PM
Last Post: league55

Forum Jump:

User Panel Messages

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