Private attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Starks

    Private attribute

    I have a class with an attribute called 'gridsize' and I want
    a derived class to force and keep it at 0.8 (representing 8mm).

    Is this a correct, or the most pythonic approach?

    ############### #####

    def __getattr__(sel f,attrname):
    if attrname == 'gridsize':
    return 0.8

    def __setattr__(sel f,attrname,valu e):
    if attrname == 'gridsize':
    pass
    else:
    self.__dict__[attrname]=value

    ############### ######


    Cheers,
    Ken.
  • Ken Starks

    #2
    Re: Private attribute

    Ken Starks wrote:
    I have a class with an attribute called 'gridsize' and I want
    a derived class to force and keep it at 0.8 (representing 8mm).
    >
    Is this a correct, or the most pythonic approach?
    >
    ############### #####
    >
    def __getattr__(sel f,attrname):
    if attrname == 'gridsize':
    return 0.8
    >
    def __setattr__(sel f,attrname,valu e):
    if attrname == 'gridsize':
    pass
    else:
    self.__dict__[attrname]=value
    >
    ############### ######
    >
    >
    Cheers,
    Ken.
    Perhaps I should mention the alternative I had in mind:

    ############### ####

    class xyz:
    def __init__(self):
    self.__dict__['a'] = 123
    self.b=456


    def __setattr__(sel f,attrname,valu e):
    if attrname == 'a':
    pass
    else:
    self.__dict__[attrname]=value

    # __getattr__() not redefined.


    ############### #############

    Comment

    • castironpi

      #3
      Re: Private attribute

      On Aug 25, 2:09 pm, Ken Starks <stra...@lampsa cos.demon.co.uk wrote:
      Ken Starks wrote:
      I have a class with an attribute called 'gridsize' and I want
      a derived class to force and keep it at 0.8 (representing 8mm).
      >
      Is this a correct, or the most pythonic approach?
      >
      ############### #####
      >
          def __getattr__(sel f,attrname):
              if attrname == 'gridsize':
                  return 0.8
      >
          def __setattr__(sel f,attrname,valu e):
              if attrname == 'gridsize':
                  pass
              else:
                  self.__dict__[attrname]=value
      >
      ############### ######
      >
      Cheers,
      Ken.
      >
      Perhaps I should mention the alternative I had in mind:
      >
      ############### ####
      >
      class xyz:
         def __init__(self):
           self.__dict__['a'] = 123
           self.b=456
      >
         def __setattr__(sel f,attrname,valu e):
               if attrname == 'a':
                   pass
               else:
                   self.__dict__[attrname]=value
      >
         # __getattr__() not redefined.
      >
      ############### #############
      This is just me, but I don't think that Python is the right language
      for your program. In Python it's extra easy to get around that
      obstacle. Python is more about freedom and discipline.

      Comment

      • =?ISO-8859-1?Q?Andr=E9?=

        #4
        Re: Private attribute

        On Aug 25, 3:47 pm, Ken Starks <stra...@lampsa cos.demon.co.uk wrote:
        I have a class with an attribute called 'gridsize' and I want
        a derived class to force and keep it at 0.8 (representing 8mm).
        >
        Is this a correct, or the most pythonic approach?
        >
        ############### #####
        >
             def __getattr__(sel f,attrname):
                 if attrname == 'gridsize':
                     return 0.8
        >
             def __setattr__(sel f,attrname,valu e):
                 if attrname == 'gridsize':
                     pass
                 else:
                     self.__dict__[attrname]=value
        >
        ############### ######
        >
        Cheers,
        Ken.
        Why not make gridsize a property with no set method?

        André

        Comment

        • Ken Starks

          #5
          Re: Private attribute

          André wrote:
          On Aug 25, 3:47 pm, Ken Starks <stra...@lampsa cos.demon.co.uk wrote:
          >I have a class with an attribute called 'gridsize' and I want
          >a derived class to force and keep it at 0.8 (representing 8mm).
          >>
          >Is this a correct, or the most pythonic approach?
          >>
          >############## ######
          >>
          > def __getattr__(sel f,attrname):
          > if attrname == 'gridsize':
          > return 0.8
          >>
          > def __setattr__(sel f,attrname,valu e):
          > if attrname == 'gridsize':
          > pass
          > else:
          > self.__dict__[attrname]=value
          >>
          >############## #######
          >>
          >Cheers,
          >Ken.
          >
          Why not make gridsize a property with no set method?
          >
          André
          Thanks for the suggestion, André.

          I admit I haven't used properties, and had to look them up.
          Pretty cool indeed ! But an extra unnecessary level of
          complexity for my needs here, I feel.

          They are _certainly_ going to become part of my
          Python toolkit.


          Cheers,
          Ken.

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Private attribute

            On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote:
            >>############# #######
            >>>
            >> def __getattr__(sel f,attrname):
            >> if attrname == 'gridsize':
            >> return 0.8
            >>>
            >> def __setattr__(sel f,attrname,valu e):
            >> if attrname == 'gridsize':
            >> pass
            >> else:
            >> self.__dict__[attrname]=value
            >[…]
            >
            I admit I haven't used properties, and had to look them up. Pretty cool
            indeed ! But an extra unnecessary level of complexity for my needs
            here, I feel.
            Compare this with your approach above and point out the extra complexity
            please:

            @property
            def gridsize(self):
            return 0.8

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • Ken Starks

              #7
              Re: Private attribute

              Marc 'BlackJack' Rintsch wrote:
              On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote:
              >
              >>>############ ########
              >>>>
              >>> def __getattr__(sel f,attrname):
              >>> if attrname == 'gridsize':
              >>> return 0.8
              >>>>
              >>> def __setattr__(sel f,attrname,valu e):
              >>> if attrname == 'gridsize':
              >>> pass
              >>> else:
              >>> self.__dict__[attrname]=value
              >>[…]
              >I admit I haven't used properties, and had to look them up. Pretty cool
              >indeed ! But an extra unnecessary level of complexity for my needs
              >here, I feel.
              >
              Compare this with your approach above and point out the extra complexity
              please:
              >
              @property
              def gridsize(self):
              return 0.8
              >
              Ciao,
              Marc 'BlackJack' Rintsch
              mea culpa.
              As i mentioned, I haven't used them before.

              I have already changed my
              class Foo: to class Foo(object):

              and I'll do the rest tomorrow.


              Comment

              • Steven D'Aprano

                #8
                Re: Private attribute

                On Mon, 25 Aug 2008 19:47:38 +0100, Ken Starks wrote:
                I have a class with an attribute called 'gridsize' and I want a derived
                class to force and keep it at 0.8 (representing 8mm).
                >
                Is this a correct, or the most pythonic approach?
                Others have already suggested using a property, but I'll suggest that the
                most Pythonic approach is not to be so dogmatic about trying to force it
                to stay at 0.8. Maybe *you* will never want it to change, but can you be
                sure that those calling your class will never want a 9mm grid?

                I'd suggest marking it private by convention:


                def SomeClass(objec t):
                _gridsize = 0.8


                The leading underscore tells callers that they change the attribute at
                their own risk.

                An even more Pythonic approach is to write your class that makes no
                assumptions about gridsize, and thus explicitly supports any reasonable
                grid size.



                --
                Steven

                Comment

                • Ken Starks

                  #9
                  Re: Private attribute

                  Steven D'Aprano wrote:

                  <snip>
                  >
                  def SomeClass(objec t):
                  _gridsize = 0.8
                  >
                  >
                  The leading underscore tells callers that they change the attribute at
                  their own risk.
                  >
                  An even more Pythonic approach is to write your class that makes no
                  assumptions about gridsize, and thus explicitly supports any reasonable
                  grid size.
                  >
                  The parent class, makes no assumption about grid-size, and I have put
                  a great deal of functionality there.

                  The methods of the derived class that depend on a gridsize of 8mm are
                  mostly concerned with standard LaTeX glyphs (from a specific font)
                  at standard LaTeX sizes.

                  I am fitting a small subset of them into my grid by hand, in a
                  way that I don't think could be easily automated even if I use the
                  metric information. Not impossible, just too much hastle.

                  The general rationale of the project is 'pen-and-ink' algorithms
                  for arithmetic, on quadrille paper. It is to create figures that
                  will be imported into LaTeX later.

                  (By the way, it is perfectly easy to re-scale the figure after
                  the digits, carry-figures, and other glyphs are placed. So long
                  as you don't mind the font-size within the figure to
                  be out of kilter with the font-size of the main run of
                  LaTeX text.)

                  I hope this explains why I have decided on a Read-only attribute, the
                  first one ever, apart from a quick try-out when I started with Python.
                  And that was when Guido was still in Amsterdam.


                  Comment

                  Working...