0

sample code what i am talking about

class A:
  def __init__(self):
    self.x = 5
  def virtual__getX(self):
    return self.x
  def getY(self):
    return self.virtual__getX()+2

class B(A):
  def __init__(self):
    A.__init__(self)
  def virtual__getX(self):
    return 20

a=A()
b=B()

print(a.getY())
print(b.getY())

is there a way in python 3 to have real virtual method without this really bad name "virtual__getX" i need clear names "__getX"

Python 3.5.2 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

7
22

if i remove virtual from name the result is

Python 3.5.2 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

7
7

is there better way to write real virtual methods like in "normal language" like delphi

Livius
  • 958
  • 1
  • 6
  • 19
  • 1
    Wait, so you want to inherit from another class and don't have inheritance? What are you actually trying to achieve? It seems like you need to use properties. https://docs.python.org/3/library/functions.html#property – Andrey Shipilov Apr 24 '17 at 07:08
  • properties are usefull but not in this case, i need to have something like protected member, inheritance is important but from INSTANCE user should not call method, but question is answered - problem was in double underscore which is used by python internally – Livius Apr 24 '17 at 08:38

1 Answers1

1

You can't have a virtual function named __something. Attributes starting with a double underscore are considered private, and python internally applies name-mangling to them.

You can, however, rename virtual__getX to getX and it'll work as you expect.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • really amaizing that double underscore have some "strange" behavior here. Is there a way to leve underscore and still have possibility to do this - something like @property decorator? – Livius Apr 24 '17 at 07:17
  • @Livius You can add another 2 backslashes at the end: `__getX__`. Names like that are usually used by python's magic methods though, so it isn't considered good practice. I don't know why you want to have getter functions, but in python we usually use properties instead. (Named `x`, not `getX` or anything else.) – Aran-Fey Apr 24 '17 at 07:21
  • i prefix all methods that do much more then only read property value with "get" but for internal class usage i prefix method by __ to "say" users do not call it from instance - but i see that if i need inheritance of that method then there is no good way to do this in python. In delphi i simple write "; virtual" after method and name dose not metter – Livius Apr 24 '17 at 07:25
  • 1
    @Livius why not just use a single underscore: `_getX` – juanpa.arrivillaga Apr 24 '17 at 07:29
  • good point but now i'm worry if in python are more magic with single underscore? – Livius Apr 24 '17 at 07:34
  • @Livius No, a single underscore doesn't do anything. – Aran-Fey Apr 24 '17 at 07:35
  • @Livius No, single underscore has no magic. Double-underscore specifically does *name-mangling*. – juanpa.arrivillaga Apr 24 '17 at 07:35
  • @Livius that being said, you shouldn't use getters and setters in Python. You should use properties *and that only when you need them*. That is the beauty of Python. – juanpa.arrivillaga Apr 24 '17 at 07:36