Category : super

I am having trouble understanding how super works, I am getting this results from the code below. Working Student Student Person The code: class Person: def __init__(self, age): print("Person") self.age = age self.dead = age + 100 class Student(Person): def __init__(self, age, gpa): print("Student") Person.__init__(self, age) #super().__init__(age) #now this Student class has age and dead ..

Read more

I am building an app in Django, it uses class based views. In my views.py I have this class based view that allows to inspect details of objects in my model Product: class ProductDetailView(DetailView): queryset = Product.objects.all() template_name = "products/detail.html" def get_context_data(self, *args, **kwargs): context = super(ProductListView, self).get_context_data(*args, **kwargs) return context As I try to ..

Read more

If a parent class and child class require the same __init__, I can think of two main ways to do this… 1. Use super() to explicitly declare __init__ class Parent: def __init__(self, attr1, attr2): self.attr1 = attr1 self.attr2 = attr2 class Child(Parent): def __init__(self, attr1, attr2): super().__init__(attr1, attr2) def do_something(self): return self.attr1 + self.attr2 2. ..

Read more

class Media_Work(object): def __init__(self): _id: int = 0 _IdDict: {} _titleDict: {} class Poem(Media_Work): def __init__(self, id, title, author, content, age, mtype, verbs): super().__init__() self.id = id self.title = title self.author = author self.content = content self.age = age self.mtype = mtype self.verbs = verbs Poem._IdDict.update({id: self}) My error message: AttributeError: ‘Poem’ object has no ..

Read more

I’ve stumbled upon a somewhat confusing behaviour related to multiple inheritance. Consider the following setting: class Parent1: def __init__(self): super().__init__() print(f"Hello from Parent1") class Parent2: def __init__(self): super().__init__() print(f"Hello from Parent2") class Child(Parent1, Parent2): def __init__(self): super().__init__() print(f"Hello from Child") c = Child() I saw it already in another question that for all parent classes ..

Read more

class Tree(Rectangle): def __init__(self, win, x, y, unit): trunk = Rectangle(Point(x – unit/2, y), Point(x + unit/2, y + unit*0.825)) trunk.setFill("brown") trunk.setOutline("brown") trunk.draw(win) crown = Polygon(Point(x – unit*2, y + unit * 0.825), Point(x, y + unit * 4.575), Point(x + unit*2, y + unit * 0.825)) crown_color = random.choice(["green", "green2", "green4", "yellow", "yellow2", "yellow4", ..

Read more

I cannot understand what can cause this anomalous call of object.init I will really appreciate a help Here the code. I’m including all the code, except for useless text because I don’t know what cause the error class IdXY(bclasses.GenStructData): _dtype_names=(‘idx’, ‘x’, ‘y’); _dtype_nfld=len(_dtype_names); _dtype_formats=(str, float, float); _dtype_nlines=1; _dtype_delimiter=’ ‘; _str_format="{idx:>12} {x:10f} {y:10f}"; (idx,idn, x,y)=’-‘,0, 0.,0.; ..

Read more