Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:41:10+00:00 2026-05-26T06:41:10+00:00

i have a little problem when i try to attach a class to an

  • 0

i have a little problem when i try to attach a class to an object in python. I have a textfile ‘file’, which is seperated into a list for each line. The program will read every line and print a name and a longname. Imagine that the textfile look like this:

“John ‘\n’ John Travolta” (…)

Here’s my bit of code:

class Name:
  def __init__(self, name, longname):
   self.name=name
   self.longname=longname
  def __str__(self):
   s="Name:"+self.name
   s+="\n Longname:"+self.longname
  def func():
   for line in file:
       name=line[:1]
       longname=line['\n':]
c=Name(name, longname)
c.func()

I get the error message that my ‘name’ and ‘longname’ is not defined but I did this with my function??? 🙁

It works if I put the function outside the class, like this:

def func():
 for line in file:
  name=line[:1]
  longname=line['\n':]
  c=Name(name, longname)
  print c

Need help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:41:11+00:00Added an answer on May 26, 2026 at 6:41 am

    You need to assign something to name and longname before you can use them:

    name = 'foo'
    longname = 'foo bar'
    c=Name(name, longname)
    c.func()
    

    but I did this with my function?

    No.

    • Firstly, you didn’t call your function yet. The function call is after the line that gives the error.
    • Secondly, the function has its own scope. The name there is different from the global name.

    I don’t think it makes any sense to move your func method inside the class definition. The code that you said works is a better way to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little Problem in Blitzmax. I try to read an INI-file and
I have this little problem, that I cannot figure out which arguments to pass
I have a little problem. I try to add an element in XML with
I have a little problem with my script. When I try to run it
I have run into a rather weird little problem. In the following code I
I have a little problem that perhaps you can help me with. I try
I have a little problem with elFinder. I'll try to explain the best I
I have a little problem when I try to run my Android application. The
I've finished my C# application, but I have a little problem: When I try
i have a little problem with the media player class. I have a button,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.