Python Forum
Object Oriented programming (OOP) problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object Oriented programming (OOP) problem
#1
Lightbulb 
Hello everyone, Dear Pythonistas

I have started to learn python several months ago and got to Object Oriented programming (OOP), solved many exercises but some of them are still quite difficult due to the lack of examples.

The problem is the following:

Write a program that describes a function to create an object. An object is created on the basis of an already existing object that is passed to the function as an argument. Only those attributes from the original object that have an integer value are added to the new created object.

My code draft (Screenshot is attached below):
#Create arbitrary class
class Object1():
    def __init__(self,value1,value2,value3):
        self.v1=value1
        self.v2=value2
        self.v3=value3

#Create an arbitrary object
D=Object(2,3,"abc")

#Function to create a new object based on the previous 
def new_object(A):
    #Here we get keys
    a=[]
    a=list(A.__dict__.keys())

    for i in range(len(a)):
        if type(getattr(A, a[i])) != int:
            delattr(A, a[i])
    print(A.__dict__.keys())

#New object
Z=new_object(D)
In the function I checked the type of all attributes and delete those that are not integers.
In the end, it turns out that the new created object "Z" based on the object "D" has <class 'NoneType'> and doesn't have any attributes at all.
What is the problem?
Yoriz write May-31-2021, 01:03 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Object Oriented programming (OOP) problem - by OmegaRed94 - May-31-2021, 11:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can someone help me solve this programming problem? SuchUmami 6 968 Nov-20-2023, 10:01 AM
Last Post: EdwardMatthew
Question Linear Programming Problem Axel_LF 0 759 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,035 Sep-30-2019, 01:13 PM
Last Post: ichabod801
  Please help! Problem with my Point object itrema 2 5,291 Mar-05-2019, 09:57 AM
Last Post: itrema
  Object Oriented Programming jackbk 8 4,510 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,428 Mar-27-2018, 11:26 AM
Last Post: Larz60+
  problem with "hiding" object league55 4 3,241 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