function not being overridden in child class

Broseph

I have these two classes:

class Shape(object):
    def __init__(self, start_point, *args):
        self.vertices = []
        self.__make_vertices(start_point, *args)

    def __make_vertices(self, start_point, *args):
        print "Not Implemented: __make_vertices"

    def __getitem__(self, *args):
        return self.vertices.__getitem__(*args)

class Cube(Shape):
    def __init__(self, start_point, side_length):
        Shape.__init__(self, start_point, side_length)

    def __make_vertices(self, start_point, side_length):
        append = self.vertices.append
        start_point = Vector(*(start_point))
        i, j, k = side_length*I, side_length*J, side_length*K
        append(start_point)
        append(self.vertices[-1] - k)
        append(self.vertices[-1] - j)
        append(self.vertices[-1] + k)
        append(self.vertices[-1] - i)
        append(self.vertices[-1] - k)
        append(self.vertices[-1] + j)
        append(self.vertices[-1] + k)
        print self.vertices

When I make a new Cube, I expected that the __make_vertices function I defined in the Cube class would be called, but instead I keep getting the message that the Shape classes __make_vertices function prints out. What am I misunderstanding?

BrenBarn

You are missing name mangling:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

In other words, you should only use attribute/method names beginning with two underscores when you specifically don't want subclasses to override them, but rather to provide their own private version of them. This is rare, so usually you won't need to use double-underscore names at all.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Get child class from parent

来自分类Dev

jQuery Widget Factory - Overridden private function is not receiving expected arguments (event, ui)

来自分类Dev

表交替颜色nth-child:not(.class)

来自分类Dev

<function>不是<class>的成员

来自分类Dev

(新的Class())-> Function(); 意思是?

来自分类Dev

How to bind arguments to child function in Vue

来自分类Dev

UITableView is being reset when I use Custom Class

来自分类Dev

Check template class for existence of function

来自分类Dev

Call a function from an another class

来自分类Dev

Finding child element by class from parent with pure javascript cross browser

来自分类Dev

简单CSS的奇怪之处:.class:hover属性被#child的属性覆盖

来自分类Dev

Hide or remove elments with same class inside from no child elements

来自分类Dev

Meteor, call function in child template from parent template

来自分类Dev

为什么 .class_name:nth-child(even) 计算没有 .class_name 的元素

来自分类Dev

PHP Trying to Use a Namespaced Function (Not Class)

来自分类Dev

Create function without knowing class name

来自分类Dev

G ++未定义对class :: function的引用

来自分类Dev

PHP:动态$ this-> class-> function

来自分类Dev

如何使用clang检索class:function

来自分类Dev

passing Collection of abstract class as parameter in function

来自分类Dev

JS remove and add class timing function

来自分类Dev

jQuery: Run function if each tr has class

来自分类Dev

how to add and remove css class in javascript function

来自分类Dev

是否未定义对“ class :: function”的引用?

来自分类Dev

事件.on('click','class',function(){})中的“ this”是什么

来自分类Dev

如何在CSS中全局覆盖`class-name> *:last-child`

来自分类Dev

Can parameters from a parametrized class be used in external function definitions?

来自分类Dev

C ++错误:“没有匹配的函数来调用[class] :: [function]”

来自分类Dev

C++ type of enclosing class in static member function

Related 相关文章

  1. 1

    Get child class from parent

  2. 2

    jQuery Widget Factory - Overridden private function is not receiving expected arguments (event, ui)

  3. 3

    表交替颜色nth-child:not(.class)

  4. 4

    <function>不是<class>的成员

  5. 5

    (新的Class())-> Function(); 意思是?

  6. 6

    How to bind arguments to child function in Vue

  7. 7

    UITableView is being reset when I use Custom Class

  8. 8

    Check template class for existence of function

  9. 9

    Call a function from an another class

  10. 10

    Finding child element by class from parent with pure javascript cross browser

  11. 11

    简单CSS的奇怪之处:.class:hover属性被#child的属性覆盖

  12. 12

    Hide or remove elments with same class inside from no child elements

  13. 13

    Meteor, call function in child template from parent template

  14. 14

    为什么 .class_name:nth-child(even) 计算没有 .class_name 的元素

  15. 15

    PHP Trying to Use a Namespaced Function (Not Class)

  16. 16

    Create function without knowing class name

  17. 17

    G ++未定义对class :: function的引用

  18. 18

    PHP:动态$ this-> class-> function

  19. 19

    如何使用clang检索class:function

  20. 20

    passing Collection of abstract class as parameter in function

  21. 21

    JS remove and add class timing function

  22. 22

    jQuery: Run function if each tr has class

  23. 23

    how to add and remove css class in javascript function

  24. 24

    是否未定义对“ class :: function”的引用?

  25. 25

    事件.on('click','class',function(){})中的“ this”是什么

  26. 26

    如何在CSS中全局覆盖`class-name> *:last-child`

  27. 27

    Can parameters from a parametrized class be used in external function definitions?

  28. 28

    C ++错误:“没有匹配的函数来调用[class] :: [function]”

  29. 29

    C++ type of enclosing class in static member function

热门标签

归档