1

i am setting python class but it prints <main.deteil object at 0x03391d90

i tried to search in google but didn't helped me

Python classes

class person:
    def __init__(self,fname,lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class student(person):
    def __init__(self,fname,lname,year):
        person.__init__(self,fname,lname)
        self.graduate_year = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, self.graduate_year)

class dateils(person):
    def __init__(self,fname,lname, age, gender,):
        person.__init__(self, fname, lname)
        self.age = 17
        self.gender = "male"

    def student_details(self):
        print(self.firstname, self.lastname, self.age,self, self.gender)


#Student
x = student("Enter your name:", "Enter your lastname: ", 2019)

x.welcome()

#person details
d = dateils(input("Enter your name"), input("Enter your lastname"), input("Enter your age"), input("Enter your gender"))
d.student_details()

i expected the output of details to be entered info, but the actual output is entered name, entered lastname, entered age, and this <main.dateils object at 0x03391D90> male

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • 1
    Possible duplicate of [How to print objects of class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-objects-of-class-using-print) – MisterMiyagi Jun 11 '19 at 15:42

2 Answers2

0

In your student_details function you print an object:

def student_details(self):
    print(self.firstname, self.lastname, self.age,self, self.gender)
                                                   ^
                                                   |
HERE ----------------------------------------------+

You didn't implemented __str__ and __repr__ functions inside the class so it prints the point to memory where your object is stored (it is the basic behavior). You should delete this self or replace it with self.graduate_year to print everything you want correctly.

vurmux
  • 9,420
  • 3
  • 25
  • 45
0

*Note I am running python 3.6.7

As mentioned by vurmux you need a __str__ or__repr__ functions inside your class. What is happening is that python is printing out the object id <main.dateils object at 0x03391D90> which is gibberish to us. A odd way of getting around this though is shown below. I have implemented a __repr__ function where I define the instance of the class dateils (you might have meant details). Then I output the results. Also I might not sure if you meant to define self.age=17 but I left it as is and just fixed that one part.

class person:
    def __init__(self,fname,lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class student(person):
    def __init__(self,fname,lname,year):
        person.__init__(self,fname,lname)
        self.graduate_year = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, self.graduate_year)

class dateils(person):
    def __init__(self,fname,lname, age, gender,):
        person.__init__(self, fname, lname)
        self.age =17
        self.gender = "male"

    def student_details(self):
        print(self.firstname, self.lastname, self.age, self.gender)

    def __repr__(self):
        return str(self.firstname) + str(self.lastname) + str(self.age) + str(self.gender)

#Student
x = student("Enter your name:", "Enter your lastname: ", 2019)

x.welcome()

#person details
d = dateils(input("Enter your name"), input("Enter your lastname"), input("Enter your age"), input("Enter your gender"))
d.student_details()

See the following question for more about the two str and repr Difference between __str__ and __repr__?