Python Forum
problem with "hiding" object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with "hiding" object
#1
This is code that's supposed to generate a cipher once, then prevent the cipher from being altered or viewed, but the code doesn't work. What's wrong with it?

from cryptography.fernet import Fernet


class Cipher:

    def __init__(self, value=None):
        self.cipher = Fernet(Fernet.generate_key())

    @property
    def cipher(self):
        return self.__cipher

    @cipher.setter
    def cipher(self, value=None):
        if not self.__cipher:
            self.__init__()
        else:
            raise ValueError("Cipher may not be altered.")

    def __repr__(self):
        return "Cipher may not be viewed."
Reply
#2
(Jan-16-2018, 07:30 PM)league55 Wrote: the code doesn't work.
how exactly doesn't work? Show us how you run it, i.e. how you instantiate object of class Cipher.
If you get ant error, post the full traceback in error tags.
Reply
#3
Here is the instantiation:

cipher = Cipher()
And here's the error message:

Output:
Traceback (most recent call last): File "C:\Users\leagu\pythonprojects\costcontrol\classes\keyclass.py", line 25, in <module> cipher = Cipher(data) File "C:\Users\leagu\pythonprojects\costcontrol\classes\keyclass.py", line 7, in __init__ self.cipher = value File "C:\Users\leagu\pythonprojects\costcontrol\classes\keyclass.py", line 15, in cipher if self.cipher != None: File "C:\Users\leagu\pythonprojects\costcontrol\classes\keyclass.py", line 11, in cipher return self.__cipher AttributeError: 'Cipher' object has no attribute '_Cipher__cipher'
Reply
#4
Why do you name the attribute and the methods with the same name?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
I found out from other sources that, in Python, you can't protect an object's attributes, so what I'm trying to do won't work. Thanks for your time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hiding username and password on sql tantony 10 2,765 Oct-21-2022, 07:58 PM
Last Post: wavic
  Hiding "undesired" info Extra 4 1,747 Jan-03-2022, 08:25 PM
Last Post: Extra
Lightbulb Object Oriented programming (OOP) problem OmegaRed94 6 2,860 May-31-2021, 07:19 PM
Last Post: OmegaRed94
  Please help! Problem with my Point object itrema 2 5,221 Mar-05-2019, 09:57 AM
Last Post: itrema

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020