473,410 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 1193
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Klein | last post by:
There are 2 ways to indicate private members of classes, by prepending 1 or 2 underscore characters, the latter causing name mangling. My question is: When would I use which kind to indicate...
2
by: KN | last post by:
I've run into such problem: I have something like this: class A(object): def __init__(self): self.__value = None class B(A): def test(self):
10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
4
by: ajikoe | last post by:
Hello, I'm new to python, How can I access private field in python. thanks
1
by: Ondrej Sevecek | last post by:
Hello, I have a general problem in mind. Imagine, you have a private member and its associated property: private int member; public int Member { get return member; set {a better code...
5
by: Samuel R. Neff | last post by:
Is there a way to add the NonSerialized attribute to the "Event" fields that the VB.NET compiler creates when you declare an event? I have a class that we use with binary serialization and we...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
4
by: Steven W. Orr | last post by:
I understand that two leading underscores in a class attribute make the attribute private. But I often see things that are coded up with one underscore. Unless I'm missing something, there's a...
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.