-1

I am a newbie and have confused learning OOP on python. I am trying to inherit class also using super, but it didn't work as expected.

here my code.

parent.py

class Sale(http.Controller):

    def cart(self, **post):
        order = request.website.sale_get_order()
        ....
        return request.render("website_sale.cart", values)

child.py

import Sale as sale

class SaleExtend(sale):

    def cart(self, **post):
        if order:
        # do something
    ....

    return super(SaleExtend, self).cart(**post)

I got an error,

AttributeError: 'Sale (extended by SaleExtend)' object has no attribute 'order'

if I just use pass its work correctly, but how to get order value from a parent?

or I did it wrong.

Dicky Raambo
  • 531
  • 2
  • 14
  • 28

1 Answers1

0

You have no instance nor a class variable order:

class Sale(http.Controller):

    def cart(self, **post):
        # FUNCTION SCOPE - exists only inside the function
        order = request.website.sale_get_order()  
        ....
        return request.render("website_sale.cart", values)

Class variables are created like so:

class Ale():
    content = "Lager"     # this is shared between all instances -> "static"

Instance variables are created like so:

class Ale():
    def __init__(self, ale_type):
        self.content = ale_type       # this is different for each instance unless you 
                                      # create multiple instances with the same ale_type
                                      # but they are still "independent" of each other

Function scope variables are creates like so:

class Ale():

    # ....

    def doSomething(self):
        lenContent = len(self.content)      # function scope
        print( self.content, lenContent )

    def doElse(self):
        print(lenContent)   # NameError - does not exist in scope

See Resolution of names and Short description of the scoping rules?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69