Python Forum
name 'fullname' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name 'fullname' is not defined
#1
class Person:

    def __init__(self, first, last, fathers_name, age):
        self.first = first
        self.last = last
        self.fathers_name = fathers_name
        self.age = age

    def fullname(self):
        return '{} {} {}'.format(self.first, self.last, self.fathers_name)

class Student(Person):
    def __init__(self, first, last, fathers_name, age, grade, avg_mark):
        super().__init__(first, last, fathers_name, age)
        self.grade = grade
        self.avg_mark = avg_mark

class Workers(Person):
    def __init__(self, fisrt, last, fathers_name, age, positon, pay, wrk_exp):
        super().__init__(first, last, fathers_name, age)
        self.position = position
        self.pay = pay
        self.wrk_exp = wrk_exp

person_1 = Person('Микола', 'Кучварський', 'Миколайович', 56)
fullname(person_1)
Error:
Traceback (most recent call last): File "C:/Users/user/AppData/Local/Programs/Python/Python37-32/School 1.py", line 26, in <module> fullname(person_1) NameError: name 'fullname' is not defined
so I`m learning Python OOP and currently Im on classes, now I have this problem, any idea why it does it ?
Reply
#2
fullname is method of class Person, so you need to call it like this
person1.fullname()
you may want to print when call it
print(person1.fullname())
a bit advanced, would be to make it a property (not method). If it is too advanced, just ignore this for the moment
@property
def fullname(self):
    return '{} {} {}'.format(self.first, self.last, self.fathers_name)
if you do so, you will access it like this
person1.fullname
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-27-2018, 02:45 PM)buran Wrote: fullname is method of class Person, so you need to call it like this
person1.fullname()
you may want to print when call it
print(person1.fullname())
a bit advanced, would be to make it a property (not method). If it is too advanced, just ignore this for the moment
@property
def fullname(self):
    return '{} {} {}'.format(self.first, self.last, self.fathers_name)
if you do so, you will access it like this
person1.fullname

Thanks you helped me a lot ! I appericate it !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,902 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020