0

I am trying to create a new instance of a class in Python. I tried the solution here: Instances in python

But it didn't work. So, my main class I have

if __name__ == '__main__':
    Tim = Person
    movement = Person.walk()

Where "Person" is the name of the class that I want to create and instance of. It has methods that I want to use.

I keep getting this error though:

Undefined Variable:Person

I also tried declaring the class instance within the actual metho, not init, but I got the same error.

Any suggestions are welcome. thanks

Community
  • 1
  • 1
JustBlossom
  • 1,259
  • 3
  • 24
  • 53
  • That's an odd error. Did you import/define `Person` class properly? Plus you instantiate class by calling it: `Person()`. – freakish Mar 24 '14 at 17:17

3 Answers3

2

You just need to add some parenthesis:

Tim = Person()

This tells python to access the constructor.

The code you provided though tells me you may not have a class Person defined. Your code should have a class Person defined.

class Person:
    def walk(self):
        print "I'm walking!"

if __name__ == "__main__":
    Time = Person
    movement = Person.walk()
    print movement

or you need a call to import the class Person

from my_other_python_file import Person

if __name__ == "__main__":
    Time = Person
    movement = Person.walk()
    print movement
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
2

Correction: please use bracket after class if you want to create an instance of that class and use that instance to call the method inside the class.

if __name__ == '__main__':
    Tim = Person()
    movement = Tim.walk()

OR but less recommended

if __name__ == '__main__':
    movement = Person().walk()
James Sapam
  • 16,036
  • 12
  • 50
  • 73
  • I am getting the same error. Do the classes have to be in the same file? Maybe that is what is wrong? They are in the same directory, just different files – JustBlossom Mar 24 '14 at 17:15
  • @Blossom96 You have to `import` the `Person` class before you use it. – That1Guy Mar 24 '14 at 17:15
  • Yes if the class is not in the same file, then you need to import the module i.e file, where the class is defined. – James Sapam Mar 24 '14 at 17:15
  • After you import the module, you can use the class as `module.Class()` – James Sapam Mar 24 '14 at 17:16
  • Same thing... unresolved import. I am writing import Person at the top of my file and I have made sure that init is at the bottom of my file – JustBlossom Mar 24 '14 at 17:17
  • 1
    Could you please read this http://docs.python.org/2/tutorial/modules.html about how to import module. – James Sapam Mar 24 '14 at 17:18
  • @Blossom96 If your `Person` class is in `foo.py` then use `from foo import Person` – freakish Mar 24 '14 at 17:18
  • oh okay thanks, I will. @sjcipher thanks, your answer was right. I just copied the classes into the same file to double check. I'll vote it up when the time period is up – JustBlossom Mar 24 '14 at 17:19
0

You need to have parenthesis to call the constructor.

Tim = Person()

As you have it now, Tim = Person is interpreted as assign Tim the value of the variable Person which hasn't been defined.

clcto
  • 9,530
  • 20
  • 42