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
#2
Your function is not returning anything. Try it like this:

#Create arbitrary class
class Object():
	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())
	return Object (a[0], a[1], 0)

#New object
Z=new_object(D)
print (type (Z))
OmegaRed94 likes this post
Reply
#3
Your arbitrary class is named Object1, whereas D is declared to be of class Object. Add a 1.

Also that is way too close to a Python reserved word. Suggest using something like my_object so a typo like happened here is easier to pick up.
OmegaRed94 likes this post
Reply
#4
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.
OmegaRed94 likes this post
Reply
#5
(May-31-2021, 12:56 PM)jefsummers Wrote: Your arbitrary class is named Object1, whereas D is declared to be of class Object. Add a 1.

Also that is way too close to a Python reserved word. Suggest using something like my_object so a typo like happened here is easier to pick up.

Thank you
Reply
#6
(May-31-2021, 12:32 PM)BashBedlam Wrote: Your function is not returning anything. Try it like this:

#Create arbitrary class
class Object():
	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())
	return Object (a[0], a[1], 0)

#New object
Z=new_object(D)
print (type (Z))

Thank you very much
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can someone help me solve this programming problem? SuchUmami 6 909 Nov-20-2023, 10:01 AM
Last Post: EdwardMatthew
Question Linear Programming Problem Axel_LF 0 739 Feb-23-2023, 11:03 PM
Last Post: Axel_LF
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,985 Mar-21-2021, 05:17 PM
Last Post: yome
  Basic programming problem darek88 1 2,005 Sep-30-2019, 01:13 PM
Last Post: ichabod801
  Please help! Problem with my Point object itrema 2 5,266 Mar-05-2019, 09:57 AM
Last Post: itrema
  Object Oriented Programming jackbk 8 4,475 Aug-24-2018, 01:13 PM
Last Post: Windspar
  Object Oriented DB Interactions datasundae 2 2,385 May-25-2018, 09:51 PM
Last Post: datasundae
  Oriented Object ldthan 1 2,405 Mar-27-2018, 11:26 AM
Last Post: Larz60+
  problem with "hiding" object league55 4 3,214 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