Python help in object orientation

Asked

Viewed 111 times

1

What’s wrong with this code? Compiler points error on line 8...

class Pai(object):
    Nome='Carlos'
    Sobrenome='Maria'
    Residencia='Rio de Janeiro'
class Filha(Pai):
    Nome='Luciana'
    def __init__(self, humor):
        self.humor = humor

f = Filha('alegre')
print(f.Nome)
print(Nome.Pai)
print(f.Sobrenome)
print(f.Residencia)
print(f.olho)
  • 3

    What error is being presented? Is it compile error (type, syntax error or something like that) or execution error (incorrect result, exception, etc)? There are several problems with your code - for example, in print(Nome.Pai) - but to help you better it would be nice to know exactly what the compiler is complaining about.

  • 2

    The error is this: expected an indented block

  • I’m learning Oop in python now I came from C and get lost in Oop but I see for help

  • 1

    By any chance are you mixing spaces with tabs? The syntax of the code posted seems correct, there is no reason to give this error. I suggest checking before each codeline identada if the number of spaces/tabs is correct. Separating the upper class from the lower class by a blank line is also good (although I think that has nothing to do with the mistake).

  • The error is probably the same edentation, many beginners in python make that mistake, and line 12 would also be wrong for Nome not be defined.

  • I’ll check but thanks for your help...

  • 1

    When you have an error, paste the error message - Python says which is the error if you read the message carefully, in addition to the line number. Now - to see here you’re right - can be really mixing spaces with tabs - configure your text editor to use 4 spaces in the indentation, never "tab"(which is a different character than space)

  • Another thing -I think you’re getting caught up in your understanding of OO there- anything, have contact Inbox in my profile.

Show 3 more comments

1 answer

0

The code has some conceptual errors, see the code below:

class Pai(object):
    Nome='Carlos'
    Sobrenome='Maria'
    Residencia='Rio de Janeiro'

class Filha(Pai):
    Nome='Luciana'
    def __init__(self, humor):
        Pai.__init__(self) # Lembre-se de sempre iniciar a classe pai
        self.humor = humor

f = Filha('alegre')
print(f.Nome)
print(f.Sobrenome)
print(f.Residencia)

Other considerations: On the line print(Nome.Pai) cannot be obtained because there is no instance of Nome. If you want to find the father’s name, it will not be possible because the attribute Name was overwritten by the daughter class.

The attribute f.olho is invalid as it does not exist in the class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.