-1

I want to import a class that I have defined and use it in a different file (main.py):

this is the class i have defined:

Example.py:

class Example:
  m=0
  n=0

def _init_(self,n,m):
    self.n=n
    self.m=m

main.py

 from Example import *
 p = Example (2,3)

both of the files are at at the same directory but when I run main.py I am getting an error:

"TypeError: object() takes no parameters"

where am I wrong ?

Ohad
  • 1,563
  • 2
  • 20
  • 44
  • In error message you should have line number - maybe your problem has nothing to do with import – furas Jun 17 '14 at 11:53

2 Answers2

6

The __init__ function is a magic method, and thus, has 2 underscores before and after the word.

As other have pointed out, you methods must also be properly indented to be inside your class.

BeetDemGuise
  • 954
  • 7
  • 11
4
class Example:

    def __init__(self, n, m):
        self.n = n
        self.m = m

This is the code you want. The __init__ has double underscores and it must be proper indented.

Also, your class level m=0, n=0 does not work the way you expect. It is not a default value to the instance.

iurisilvio
  • 4,868
  • 1
  • 30
  • 36
  • don't u need to define n and m ? – Ohad Jun 17 '14 at 11:57
  • 1
    No, the definition at class level is not what you expect (like in Java for example). You don't need to define variables before set a value to them. – iurisilvio Jun 17 '14 at 12:00
  • 1
    [There is no declaration (what you apparently meant when you said "define") in Python](http://stackoverflow.com/questions/11007627/python-variable-declaration/11008311#11008311). – Karl Knechtel Jun 17 '14 at 12:26
  • Yes, Karl. "Declare" is the correct term, not "define". Thanks! – iurisilvio Jun 17 '14 at 13:39