468,738 Members | 2,670 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

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

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

#####################
Cheers,
Ken.
Aug 25 '08 #1
8 1073
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__(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.
############################
Aug 25 '08 #2
On Aug 25, 2:09*pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
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__(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.
Aug 25 '08 #3
On Aug 25, 3:47*pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
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é
Aug 25 '08 #4
André wrote:
On Aug 25, 3:47 pm, Ken Starks <stra...@lampsacos.demon.co.ukwrote:
>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.
Aug 25 '08 #5
On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote:
>>####################

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
Aug 25 '08 #6
Marc 'BlackJack' Rintsch wrote:
On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote:
>>>####################

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.
Aug 25 '08 #7
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(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.

--
Steven
Aug 26 '08 #8
Steven D'Aprano wrote:

<snip>
>
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.
Aug 26 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Daniel Klein | last post: by
2 posts views Thread by KN | last post: by
10 posts views Thread by Scott Brady Drummonds | last post: by
4 posts views Thread by ajikoe | last post: by
63 posts views Thread by time.swift | last post: by
4 posts views Thread by Steven W. Orr | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.