Common mistakes we make in Python and why we should avoid that

class MyClass(object): 
def __init__(self, name):
self.name = name
def __str__(self):
return f"MyClass from first file: {self.name}"
class MyClass(object): 
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"MyClass: {self.name} with age: {self.age}"
from first import MyClass class MyClassExtended(MyClass): 
def __str__(self):
return f"Extended class with name: {self.name}"
from second import MyClass 
from third import *
print("\nI am creating my class extended\n")
my_ex = MyClassExtended("extendedclass")
print(my_ex)
print("\nI am Expecting MyClass is from second.py\n") print("Actually my class is: ", MyClass, '\n')
my_class_2 = MyClass("Name", 2)
print(my_class_2)
afsal@afsal-Mi-NoteBook-Horizon-Edition-14:~/Desktop/experiments/import_experiment$ python3 fourth.py 
I am creating my class extended
Extended classs with name: extendedclass
I am Expecting MyClass is from second.py
Actually my class is: <class 'first.MyClass'>
Traceback (most recent call last):
File "fourth.py", line 16, in <module>
my_class_2 = MyClass("Name", 2)
TypeError: __init__() takes 2 positional arguments but 3 were given
afsal@afsal-Mi-NoteBook-Horizon-Edition-14:~/Desktop/experiments/import_experiment$
from second import MyClass 
from third import MyClassExtended
print("\nI am creating my class extended\n")
my_ex = MyClassExtended("extendedclass")
print(my_ex)
print("\nI am Expecting MyClass is from second.py\n") print("Actually my class is: ", MyClass, '\n')
my_class_2 = MyClass("Name", 2)
print(my_class_2)
afsal@afsal-Mi-NoteBook-Horizon-Edition-14:~/Desktop/experiments/import_experiment$ python3 correct.py 

I am creating my class extended

Extended classs with name: extendedclass

I am Expecting MyClass is from second.py

Actually my class is: <class 'second.MyClass'>

MyClass: Name with age: 2
afsal@afsal-Mi-NoteBook-Horizon-Edition-14:~/Desktop/experiments/import_experiment$

--

--

--

Coding, Reading,Python

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Part1: Installing Terraform & Service Automation Module on IBM Cloud Pak for MultiCloud Management

Headpose…one small step for XR…

RxJava2: Schedulers 101 or simplified concurrency, part 1.

Active Directory Kerberos authentication for Apache web server

ANTIQUE — HackTheBox WriteUp

ANON masternodes troubleshooting — what am I doing wrong that I can’t setup my masternode?

To DevOps or Not To DevOps: Expert Opinions

Agile vs Dev vs Ops

Constrained Shortest Path — Algorithms

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afsalms

Afsalms

Coding, Reading,Python

More from Medium

How to reduce lines of code [python]

Build Array from Permutation — Day 103(Python)

Coding is Important

AlphaGrep Python Developer Interview(On Campus)

AlphaGrep Logo