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 into Columns using pdfplumber arvin 17 5,200 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Data from tables DataExtrator 0 1,108 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  Extracting and printing data ajitnayak1987 0 1,387 Jul-28-2021, 09:30 AM
Last Post: ajitnayak1987
  Extracting unique pairs from a data set based on another value rybina 2 2,264 Feb-12-2021, 08:36 AM
Last Post: rybina
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 1,895 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  Extracting data without showing dtype, name etc. tgottsc1 3 4,205 Jan-10-2021, 02:15 PM
Last Post: buran
  Extracting data from a website tgottsc1 2 2,211 Jan-09-2021, 08:14 PM
Last Post: tgottsc1
  Extracting data based on specific patterns in a text file K11 1 2,180 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,406 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Extracting data from multiple txt files Emmanouil 7 5,068 Aug-25-2019, 10:24 PM
Last Post: Emmanouil

Forum Jump:

User Panel Messages

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