0

So I will have to finish a half-done code to get the desired output.

the half-done code goes as follows AND I AM NOT ALLOWED TO CHANGE THIS CODE:

class Wadiya():
def __init__(self):
self.name = 'Aladeen'
self.designation = 'President Prime Minister Admiral General'
self.num_of_wife = 100
self.dictator = True

the desired output goes as follows:

Part 1:
Name of President: Aladeen
Designation: President Prime Minister Admiral General
Number of wife: 100
Is he/she a dictator: True
Part 2:
Name of President: Donald Trump
Designation: President
Number of wife: 1
Is he/she a dictator: False

Now to get this output, I will have to use the same object which is wadiya in this case to change the values of the instance variables. Then print if it affected the previous values of Part 1. If it did, I'll have to print 'Previous information lost' otherwise I'll have to print 'No, changing had no effect in previous values.'

Now my question is, how can I change the values of the instance variables using the same object? This is what I've done, but I don't think this what the question has asked me to do. What do you think? Am I on the right track? Here's my approach:

class Wadiya():
   
    def __init__(self):
        self.name = 'Aladeen'
        self.designation = 'President Prime Minister Admiral General'
        self.num_of_wife = 100
        self.dictator = True
        
    def my_method(self):
        print('Name of the President:', self.name)
        print('Designation:', self.designation)
        print('Number of wife:', self.num_of_wife)
        print('Is he/she a dictator:', self.dictator)
        
    def change_values(self, name, designation, num_of_wife, dictator):
        self.name = name
        self.designation = designation
        self.num_of_wife = num_of_wife
        self.dictator = dictator
        

print('Part 1:')        
wadiya = Wadiya()
wadiya.my_method()
print('Part 2:')
wadiya = Wadiya()
wadiya.change_values('Donald Trump', 'President', 1, False)
wadiya.my_method()
2
  • Did you try it? Commented Mar 16, 2021 at 6:59
  • I have included my approach..and i got the same output. But I'm not sure if my approach is correct because the question says to change the values using the same object whereas I used methods Commented Mar 16, 2021 at 7:05

1 Answer 1

1

Question is a bit ambiguous why would you want to change all values of an instance. If you want you can reassign new instance to same variable just pass arguments to init instead of change_method

  1. if you want default values to class then you don't need to do init and then change values.

    def __init__(self, name: str = None): # None is default value
        self.name: str = name if name else 'Aladeen'
    
  2. For some reason if you want to change values of instanced objects then do

    wadiya.name = 'Donald'
    

what you are doing will work, but generally not suggested

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.Well, that's what the question wants. It wants me to change all the values. Also, I'm not allowed to change ANYTHING in the given half-done code. I've also mentioned that in bold

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.