-1

I need to Create a class called Color.

  • Code 1 constructor
  • That requires 2 parameters
  • Code 2 properties
  • 1 property is public
  • 1 property is private
  • Create an accessor and setter for the 1 private property
  • Instantiate the class created above
  • Output the two properties

I believe I have been able to create the class and as well the constructor but where am having my issues is the public/private properties as well as the other parts are troubling me.

class Color:
   def __init__(self,full_name,birthday ):
       self.name = full_name
       self.birthday = birthday

   def age(self):
      return self.birthday

   def fname(self):
       return _self.name

object = Color()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • def age and def fname should be into class – CuCaRot Jul 18 '19 at 02:51
  • 2
    This is not an answer, but it is relevant and you should read it: https://stackoverflow.com/a/4555970/5709499 – Calder White Jul 18 '19 at 02:53
  • 1
    I think this is also worth a read: [Does Python have “private” variables in classes?](https://stackoverflow.com/q/1641219/2745495). – Gino Mempin Jul 18 '19 at 02:56
  • 1
    `return _self.name` ← this will raise an error. Did you mean `return self._name` instead? – TrebledJ Jul 18 '19 at 02:58
  • This uses the term "properties", but the wording makes it sound like they mean "attributes". I'd suggest clarifying the assignment's intent; attributes are actual values stored in an instance, properties are accessors that are used like attributes, but invoke functions to satisfy them, rather than just looking up a value on the instance directly. – ShadowRanger Jul 18 '19 at 03:05
  • yes @TrebledJ self._name my mistake – Ben Franklin Jul 18 '19 at 03:05

1 Answers1

1

Using python @property getters and setters decorators, the calls to these from an instance are undistinguishable.
The inner workings follow the leading underscore convention for private attributes, without resorting to double underscore name mangling.

class Homework:

    def __init__(self, param1, param2):
        self._param1 = param1    # private attribute
        self.param2 = param2     # public attribute

    @property
    def param1(self):
        """accessor for private parameter
        """
        return self._param1

    @param1.setter
    def param1(self, value):
        """setter for private parameter
        """ 
        self._param1 = value

homework = Homework('The great secret', 'The public life of Napoleon')

# accessing the parameters follow the same syntax
print(homework.param1)
print(homework.param2)

# setting the parameters also follow the dotted syntax
homework.param1 = "you won't believe it"    
homework.param2 = 'but the dog ate it'    

print(homework.param1)
print(homework.param2)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 2
    Note: If they truly mean *private*, not just "non-public"/protected by convention, then it should be `self.__param1` (two underscores), not `self._param1` (one underscore). Single underscore means "protected" or "not part of public API", where double underscore employs name-mangling to separate the attribute name based on the class name. – ShadowRanger Jul 18 '19 at 03:32
  • Yes, agreed - if they truly mean private, then maybe python is not a good choice. – Reblochon Masque Jul 18 '19 at 05:36