1

in school we got this class file:

class Konto:
    def __init__(self, nummer):
        self.__nr = nummer
        self.__stand = 0
        self.__minimum = -1000.0

    def getStand(self):
        return self.__stand

    def getNr(self):
        return self.__nr

    def einzahlen(self, betrag):
        self.__stand = self.__stand + betrag

    def auszahlen(self, betrag):
        if self.__stand - betrag >= self.__minimum:
            self.__stand = self.__stand - betrag
        else:
            print("Auszahlung nicht möglich!")

class Sparkonto(Konto):
    def __init__(self, nummer):
        Konto.__init__(self, nummer)
        self.__zinssatz = None
        self.__minimum = 0
        self.__maxAuszahlung = 2000.0

    def setZinssatz(self, zinssatz):
        self.__zinssatz = zinssatz

    def getZinssatz(self):
        return self.__zinssatz

    def auszahlen(self, betrag):
        if betrag <= self.__maxAuszahlung:
            Konto.auszahlen(self, betrag)
        else:
            print("Auszahlung nicht möglich!")

    def zinsenGutschreiben(self):
        zinsen = self.__stand * (self.__zinssatz / 100)
        self.einzahlen(zinsen)

When I run this test programm:

#Test
from sparkonto import *
s = Sparkonto(1)
s.einzahlen(1000)
print(s.getStand())
s.setZinssatz(4)
print(s.getZinssatz())
s.zinsenGutschreiben()
print(s.getStand())
s.auszahlen(2500)
print(s.getStand())

I get this error

1000
4
Traceback (most recent call last):
  File "/home/malte/home/py3/sparkonto/test.py", line 8, in <module>
    s.zinsenGutschreiben()
  File "/home/malte/home/py3/sparkonto/sparkonto.py", line 44, in zinsenGutschreiben
AttributeError: 'Sparkonto' object has no attribute '_Sparkonto__einzahlen'
>>> 

We do not know what we are doing wrong. Any guess?

3
  • I think you should try as much as possible to write your code using english Commented Jun 22, 2010 at 6:23
  • @daniel: we got this from our teacher and I think she does this because most of my class is to dumb Commented Jun 22, 2010 at 6:39
  • ok I see, but for your future development I strongly recommend you to use english :) Commented Jun 22, 2010 at 6:49

3 Answers 3

2

Daniel was halfway there, you do need to change self.__einzahlen -> self.einzaheln, as he said.

Also, self.__stand belongs to the parent class. With the double underscore in the name, it gets mangled used anywhere else. But you don't need to use self.__stand directly. Konto gives you getStand().

Try something like this:

def zinsenGutschreiben(self):
    zinsen = self.getStand() * (self.__zinssatz / 100)
    self.einzahlen(zinsen)
Sign up to request clarification or add additional context in comments.

3 Comments

just to be sure that I've understood. If you prefix your variable names with '__' than your variable cannot be accessed from derived classes? It's like private in C++?
@Daniel, not quite, the name of the attribute is mangled to eg. _Konto__stand and is perfectly accessable under that name. It does ensure that a subclass doesn't accidently overwrite that name.
It can be accessed, but with a double underscore, the name is mangled when used from the derived classes. It's just a convention, giving you a hint that the author wants to treat it as "private". In this case, you could have also used "self._Konto__stand", but why bother when there's an "official" getStand() function?
1

self.__einzahlen(zinsen) -> self.einzahlen(zinsen)

4 Comments

why not? __einzahlen function is not defined in base class.
@Malte: There is no such thing as "private" in Python.
we learned that leading double underscore means private but i often don't think that my teacher knows how python works
Leading double underscores does not mean private. There is no such thing as "private" in Python.
1

Double leading underscores invoke name mangling, using the current class's name. Use a single leading underscore instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.