Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Python quesiton: superclass.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Sat May 14, 2016 2:26 pm    Post subject: Python quesiton: superclass. Reply with quote

Please take a look at the code below. I create a superclass "Animal" and a class "Dog" that inherit from "Animal" However, I kept getting an error message, which is in the screenshot. Based on the error message, it looks like the class "Dog" did not get the attributes from superclass "Animal". Thanks in advance.
Code:

class Animal:
    __name = ""
    __height = 0
    __sexuality = ""

    def __init__(self, name, height, sexuality):
        self.__name = name
        self.__height = height
        self.__sexuality = sexuality


    def set_name(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

    def set_height(self, height):
        self.__height = height

    def get_height(self):
        return self.__height

    def set_sexuality(self, sexuality):
        self.__sexuality = sexuality

    def get_sexuality(self):
        return self.__sexuality

    def get_type(self):
        return "Animal"

    def toString(self):
        return "{} is {} cm tall and it is a {}".format(self.__name,
                                                     self.__height,
                                                     self.__sexuality)


class Dog(Animal):
    __owner = ""

    def __init__(self, name, height, sexuality, owner):
        self.__owner = owner
        super(Dog, self).__init__(name, height, sexuality)

    def set_owner(self,owner):
        self.__owner = owner

    def get_owner(self):
        return self.__owner

    def get_type(self):
        return "Dog"

    def toString(self):
        return "{} is a dog, it is {} cm tall, it is a {} and {} is his owner".format(self.__name,  <---- This is line 56
                                                                                      self.__height,
                                                                                      self.__sexuality,
                                                                                      self.__owner)

dog = Dog("Lab", 30, "male", "Chris")


print(dog.toString())   <----This is line 64.



Screen Shot 2016-05-14 at 2.31.52 PM.png
 Description:
 Filesize:  80.05 KB
 Viewed:  4546 Time(s)

Screen Shot 2016-05-14 at 2.31.52 PM.png


Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4709

PostPosted: Sat May 14, 2016 5:45 pm    Post subject: Reply with quote

Haven't studied python, but after taking 10 seconds to use google:
Quote:
This brings us to the double underscore prefix. "__privateMethod" can also be accessed from outside the class, but to do so, you need to write "object._ClassName__privateMethod." This makes it a bit more difficult to access said method/field, but not impossible. Also, the method/field name is mangled with the class in which that method/field is defined, not in any subclasses.
Source: https://www.quora.com/What-are-the-access-modifiers-in-python
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Mon May 16, 2016 12:23 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Haven't studied python, but after taking 10 seconds to use google:
Quote:
This brings us to the double underscore prefix. "__privateMethod" can also be accessed from outside the class, but to do so, you need to write "object._ClassName__privateMethod." This makes it a bit more difficult to access said method/field, but not impossible. Also, the method/field name is mangled with the class in which that method/field is defined, not in any subclasses.
Source: https://www.quora.com/What-are-the-access-modifiers-in-python


Thanks for the reply, but I still don't know where I did wrong, Sad
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Mon May 16, 2016 8:54 pm    Post subject: Reply with quote

Sounds like it's suggesting:
Code:
self._Animal__name

Or get rid of all the underscores for your variables...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites