Private attribute

K

Ken Starks

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__(self,attrname):
if attrname == 'gridsize':
return 0.8

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

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


Cheers,
Ken.
 
K

Ken Starks

Ken said:
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__(self,attrname):
if attrname == 'gridsize':
return 0.8

def __setattr__(self,attrname,value):
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__(self,attrname,value):
if attrname == 'a':
pass
else:
self.__dict__[attrname]=value

# __getattr__() not redefined.


############################
 
C

castironpi

Ken said:
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__(self,attrname):
        if attrname == 'gridsize':
            return 0.8
    def __setattr__(self,attrname,value):
        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__(self,attrname,value):
         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.
 
A

André

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__(self,attrname):
         if attrname == 'gridsize':
             return 0.8

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

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

Cheers,
Ken.

Why not make gridsize a property with no set method?

André
 
K

Ken Starks

André said:
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__(self,attrname):
if attrname == 'gridsize':
return 0.8

def __setattr__(self,attrname,value):
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.
 
M

Marc 'BlackJack' Rintsch

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

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

def __setattr__(self,attrname,value):
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
 
K

Ken Starks

Marc said:
####################

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

def __setattr__(self,attrname,value):
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.
 
S

Steven D'Aprano

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(object):
_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.
 
K

Ken Starks

Steven D'Aprano wrote:

def SomeClass(object):
_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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top