-1

I want to make a Person class containing first_name and last_name attributes that are passed into the __init__ method. I then want to add a property called full_name that returns the first and last names together with a space between them. I then also want to add a property called name that returns the names together in the format of last name, followed by a comma and a space, followed by the first name.

An example output is below.

 from Code4 import Person
    teacher = Person("D", "C")
    teacher.last_name
'C'
    teacher.full_name
'D C'
    teacher.name
'C, D'

I am confused on how to write the code for this sort of output.

melpomene
  • 84,125
  • 8
  • 85
  • 148
user10019227
  • 117
  • 1
  • 2
  • 7

4 Answers4

1

Using @property decorator (that way you can type only Person.name and not Person.name() with parenthesis:

class Person:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @property
    def full_name(self):
        return f'{self.first_name} {self.last_name}'

    @property
    def name(self):
        return f'{self.last_name}, {self.first_name}'

teacher = Person('D', 'C')
print(teacher.last_name)
print(teacher.full_name)
print(teacher.name)

Prints:

C
D C
C, D
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Also what do the f' do after the returns? – user10019227 Jul 22 '18 at 17:54
  • @user10019227 That's Python f-string formatting, introduced in Python3.6 (If you use lesser version, you can use `'{} {}'.format(self.first_name, self.last_name)` for example. – Andrej Kesely Jul 22 '18 at 17:58
  • ah that's new I see. so using property decorator we can call methods like attributes ? @AndrejKesely – YOLO Jul 22 '18 at 19:39
  • @YOLO yes, you can make getters and setters this way easily https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work – Andrej Kesely Jul 22 '18 at 19:41
1

You can also try the below code.

Try code nline at http://rextester.com/ILKVMO81621

class Person(object):
    def __init__(self, first_name, last_name):
        """
        Constructor function that sets properties
        """
        self.first_name = first_name
        self.last_name = last_name
        self.full_name = self.first_name + " " + self.last_name
        self.name = self.last_name + ", " + self.first_name

if __name__ == "__main__":
    person1 = Person("Anders", "Rossum")
    print(person1.full_name) # Anders Rossum
    print(person1.name)      # Rossum, Anders
hygull
  • 8,464
  • 2
  • 43
  • 52
0

You can create methods inside your class called full_name and name that, when called on your "teacher" object, will print out the output you want. In the init() function, the inputs "D" and "C" that are passed in are assigned to variables, and you can simply print out or return those variables in a specific fashion to achieve the output you want.

See this code for reference:

class Person:

    def __init__(self, first, last):
        self.__first = first;
        self.__last = last;

    def full_name():
        print(self.__first + " " + self.__last)

    def name():
        print(self.__last + ", " + self.__first)

Please don't hesitate to correct me if some syntax is wrong.

ab123
  • 357
  • 4
  • 17
0

This is how I would solve this problem.

class Person:

    def __init__(self,first_Name,last_Name):
        self.firstName = first_Name
        self.lastName = last_Name

    def full_name(self):
        fullname = self.firstName + ' ' + self.lastName
        print(fullname)

    def name(self):
        name = self.lastName + ', ' + self.firstName
        print(name)

person1 = Person('D','C')
print(person1.lastName)
person1.full_name()
person1.name()
Kiran
  • 55
  • 1
  • 7