Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Compile error
#1
I have simple program running on pyscripter 2.5 and similar code tried on jupyter notebook.
How to resolve below error

reference code taken from https://codeigo.com/python/import-class-...-directory

car.py
class Car:
    def init(self, company, model, year):
        self.company = company
        self.model = model
        self.year = year
    def get_details(self):
        details = str(self.year) + ' ' + self.company + ' ' + self.model
        return details
main.py
from car import Car
print(Car)
print('enter the code')
mycar = Car('Ford', 'Escort', 2000)
error
Output:
<class 'car.Car'> enter the code Traceback (most recent call last): File "C:\Users\python\main.py", line 5, in <module> mycar = Car('Ford', 'Escort', 2000) TypeError: Car() takes no arguments
Reply
#2
note, it should be __init__, not init
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks It worked. I have similar example
complexnumber.py
class Complex:
    def __init__(self,real,imaginary):
        self.real=real
        self.imaginary=imaginary   
    def __add__(self,right):
        return Complex(self.real+right.real,self.imaginary+right.imaginary)
    def __iadd__(self,right):
        self.real+=right.real
        self.imaginary+=right.imaginary
        return self
    def __repr__(self):
        return (f'({self.real}'+('+' if self.imaginary>=0 else '-')+
               f'{abs(self.imaginary)}i)')
        
But when try using ipython module i get error. But it work on pyscripter
Output:
from complexnumber import complex --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-470da91856be> in <module> ----> 1 from complexnumber import complex ~\Pictures\Pythoncode\Class\complexnumber.py in <module> 52 { 53 "cell_type": "code", ---> 54 "execution_count": null, 55 "id": "dc586128-a712-4774-9f79-6d36de5408b3", 56 "metadata": {}, NameError: name 'null' is not defined
Reply
#4
You are not showing the code where the error occurs. Can you paste more (all) of your code?
Reply
#5
(Jun-03-2021, 10:16 AM)ajitnayak1987 Wrote:
Output:
from complexnumber import complex--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-470da91856be> in <module> ----> 1 from complexnumber import complex
The name of the class is "Complex". Not "complex". Remember Python is case sensitive.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,319 May-24-2022, 07:02 AM
Last Post: netanelst
  compile error Visual Studio Code jamie_01 2 1,693 Jan-25-2022, 09:36 AM
Last Post: Larz60+
  cv2-python will n ot compile?? barryjo 5 2,189 Dec-26-2021, 06:42 PM
Last Post: barryjo
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,473 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Python/C API project - compile to exe MikeTrusky 0 1,519 Jul-22-2020, 03:55 PM
Last Post: MikeTrusky
  How to cross compile python for ARM ? pankaj 4 5,689 Mar-06-2019, 05:59 AM
Last Post: pankaj
  Compile python project to single portable .exe file Campbell 5 10,694 Dec-05-2017, 01:20 PM
Last Post: buran

Forum Jump:

User Panel Messages

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