473,387 Members | 1,650 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,387 software developers and data experts.

Catching errors in attribute names at assigment

If one makes a typographical error when assigning a value to an
attribute (i.e if one types ClassName.xyc = 10 instead of
ClassName.xyz=10), a new attribute xyc will be created, and the error
can later prove difficult to debug.

Is there a way to check assignments when they are made and to allow
only those that modify a restricted set of allowable attributes?
Looking through ClassName.__dict__.keys() is perhaps one way to
achieve this: Is there a simpler (i.e. more Pythonic) way to do it as
well?

Thomas Philips
Jul 18 '05 #1
4 1239
Thomas Philips wrote:
If one makes a typographical error when assigning a value to an
attribute (i.e if one types ClassName.xyc = 10 instead of
ClassName.xyz=10), a new attribute xyc will be created, and the error
can later prove difficult to debug. Is there a way to check assignments when they are made and to allow
only those that modify a restricted set of allowable attributes?
Looking through ClassName.__dict__.keys() is perhaps one way to
achieve this: Is there a simpler (i.e. more Pythonic) way to do it as
well?


Yes, there are at least two ways

The "new" way is to use "new classes" - ones being children of "object"
class - where you have a special attribute __all__ .

If you say __all__=["a","b","c"], those 3 attributes are the only ones valid
for field/method accessing. So class.a=5 is ok, but class.d=5 raises
exception.

The "old" way is to override __getattr__ and __setattr__ and check for a
valid name before accessing.

--
Pawel Kraszewski FreeBSD/Linux

E-Mail/Jabber Phone ICQ GG
Pa**************@wp.pl +48 604 777447 45615564 69381
Jul 18 '05 #2
On Mon, 21 Jun 2004 22:01:31 +0200,
Pawel Kraszewski <pa**************@wp.pl> wrote:
If you say __all__=["a","b","c"], those 3 attributes are the
only ones valid for field/method accessing. So class.a=5 is ok,
but class.d=5 raises exception.


ITYM __slots__ instead of __all__.

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #3
Heather Coppersmith wrote:
If you say __all__=["a","b","c"], those 3 attributes are the
only ones valid for field/method accessing. So class.a=5 is ok,
but class.d=5 raises exception.
ITYM __slots__ instead of __all__.


My fault, __all__ is for module members, not class members.

Sorry for that.

--
Pawel Kraszewski FreeBSD/Linux

E-Mail/Jabber Phone ICQ GG
Pa**************@wp.pl +48 604 777447 45615564 69381
Jul 18 '05 #4
tk****@hotmail.com (Thomas Philips) wrote in
news:b4*************************@posting.google.co m:
Is there a way to check assignments when they are made and to allow
only those that modify a restricted set of allowable attributes?
Looking through ClassName.__dict__.keys() is perhaps one way to
achieve this: Is there a simpler (i.e. more Pythonic) way to do it as
well?


Two more Pythonic ways to do this:

Write lots of unit tests. That way you not only catch this specific problem
but lots of others as well.

Use PyChecker. That way you not only catch this specific problem but lots
of others as well.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
6
by: Coral Snake | last post by:
I am having problems with programming even simple "Hello World" programs from books and tutorials that use Python GUI libraries. Such Programs cause python to throw "Attribute Errors" even when the...
2
by: yopwojtek | last post by:
Hello All, i know from some tutorials, that copy constructor and assigment operator is not inherited from base to derived class. i think it make sense but i wrote a little example: class Base {...
14
by: Brandon | last post by:
I am an amateur working on a first site, I have settled on using FP 2002 for now. My current page is up and live, but I have two errors that I cant seem to get rid of ... Line 29, column 6:...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
7
by: jm.suresh | last post by:
Hi, In the following program, I have a class Test which has a property x. Its setx function gets a string value and converts it into a float and stores into it. class Test(object): def...
4
by: John Pye | last post by:
Hi all I have some C code that is giving me some 'nan' values in some calculations. The C code is wrapped using SWIG to give me a Python module that I am then exercising through a unittest...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.