Please Note: This is a small, similar example of something I'm trying to do for a school assignment and therefore certain pieces of the solution cannot be altered. Those pieces are pointed out.
It is my understanding that str can be used to format how a class is represented when printed.
I am intending to overwrite the str method so that:
>>>print(class_slim)
my name is... Slim Shady
and
>>>print(class_fat)
my name is... Fat Shady
Here is the example code:
class MyClass:
fname = ''
lname = ''
def __init__(self, fname='', lname=''):
self.name = ""
def __str__(self):
return 'my name is... '+self.fname, self.lname
@staticmethod
def setname(first, last):
return first, last
class_slim = MyClass.setname("Slim", "Shady")
print(class_slim)
class_fat = MyClass.setname("Fat", "Shady")
print(class_fat)
Right now, I am getting this result:
>>>class_slim = MyClass.setname("Slim", "Shady")
>>>print(class_slim)
('Slim', 'Shady')
I am required to:
- use static method (setname) to change fname and lname,
- different instances of class would need to return different strings (I want to output "my name is... Slim Shady" and "my name is... Fat Shady"...
If any pieces of this are impossible or assumptions I've made are downright wrong please let me know, that may be the issue.