Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extracting data
#1
I am new bie. I have written small code here. how can I print first name from function directly from instance


class Test:
    def __init__(self,first,last):
        self.first=first
        self.last=last
    def display_first(self):
        return '{}'.format(self.first)

    def display_Last(self):
        print("Last name:",self.last)

Emp1=Test

Emp1.first="Ajit"
Emp1.last="Nayak"

print(Test.display_first(Emp1))
print(Emp1.display_first())
#Emp1.display_first()
Reply
#2
You're not calling the constructor properly. You're setting Emp1 to the class itself, not an instance of the class.

I would prefer for something like this to not bother with a method, just print the attribute directly. But you can create a method to return or to print it. Here's a simplification of what you were doing earlier.

class Test:
    def __init__(self,first,last):
        self.first=first
        self.last=last

    def display_first(self):
        return '{}'.format(self.first)

    def display_Last(self):
        print("Last name:",self.last)

Emp1=Test("Ajit", "Nayak")

print(Emp1.first)
print(Emp1.display_first())
Output:
Ajit Ajit
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting data from bank statement PDFs (Accountant) a4avinash 4 4,961 Feb-27-2025, 01:53 PM
Last Post: griffinhenry
  Confused by the different ways of extracting data in DataFrame leea2024 1 639 Aug-17-2024, 01:34 PM
Last Post: deanhystad
  Extracting the correct data from a CSV file S2G 6 1,739 Jun-03-2024, 04:50 PM
Last Post: snippsat
  Extracting Data into Columns using pdfplumber arvin 17 15,036 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Data from tables DataExtrator 0 1,605 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  Extracting and printing data ajitnayak1987 0 1,814 Jul-28-2021, 09:30 AM
Last Post: ajitnayak1987
  Extracting unique pairs from a data set based on another value rybina 2 3,011 Feb-12-2021, 08:36 AM
Last Post: rybina
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 2,493 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  Extracting data without showing dtype, name etc. tgottsc1 3 8,542 Jan-10-2021, 02:15 PM
Last Post: buran
  Extracting data from a website tgottsc1 2 2,897 Jan-09-2021, 08:14 PM
Last Post: tgottsc1

Forum Jump:

User Panel Messages

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