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 a user-completed fillable pdf Perry 2 1,541 Sep-25-2025, 01:49 PM
Last Post: DeaD_EyE
  Extracting data from bank statement PDFs (Accountant) a4avinash 4 15,398 Feb-27-2025, 01:53 PM
Last Post: griffinhenry
  Confused by the different ways of extracting data in DataFrame leea2024 1 1,304 Aug-17-2024, 01:34 PM
Last Post: deanhystad
  Extracting the correct data from a CSV file S2G 6 2,948 Jun-03-2024, 04:50 PM
Last Post: snippsat
  Extracting Data into Columns using pdfplumber arvin 17 36,823 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Data from tables DataExtrator 0 2,120 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  Extracting and printing data ajitnayak1987 0 2,224 Jul-28-2021, 09:30 AM
Last Post: ajitnayak1987
  Extracting unique pairs from a data set based on another value rybina 2 3,613 Feb-12-2021, 08:36 AM
Last Post: rybina
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 3,066 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  Extracting data without showing dtype, name etc. tgottsc1 3 11,221 Jan-10-2021, 02:15 PM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.