473,796 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

classic and property() version of same attribute - yikes!


I just stumbled upon a bug in some group-written code. We have this sort of
class hierarchy:

class X(object):
...
class A(X):
def __init__(...):
self.attr = 0.0

def GetAttr(self):
return self.attr

def SetAttr(self, a):
self.attr = a
class B(A):
...
class C(B):
def set_attr(self, a):
...

def get_attr(self):
...

attr = property(get_at tr, set_attr, None, None)

Each of the four classes is defined in a different module. The author of A
was not aware that it was a new-style class and coded it like a classic
class. The author of C (different person) apparently knew it was new-style
and took advantage of that fact to use property(), but didn't notice there
was already an attribute named "attr" two levels up.

This of course all gets to the Zen bit: "Flat is better than nested", and
I'll use that as an argument for flatter hierarchies in the future.
Nonetheless, what would be the effect of such an attribute stomping? Do I
just have multiple ways to change self.attr, assuming __slots__ hasn't been
declared? This seems like something perhaps pychecker should notice, but
can it?

Thx,

Skip
Jul 18 '05 #1
1 1409
Skip Montanaro <sk**@pobox.com > wrote in message news:<ma******* *************** *************** @python.org>...
I just stumbled upon a bug in some group-written code. We have this sort of
class hierarchy:

class X(object):
...
class A(X):
def __init__(...):
self.attr = 0.0

def GetAttr(self):
return self.attr

def SetAttr(self, a):
self.attr = a
class B(A):
...
class C(B):
def set_attr(self, a):
...

def get_attr(self):
...

attr = property(get_at tr, set_attr, None, None)

Each of the four classes is defined in a different module. The author of A
was not aware that it was a new-style class and coded it like a classic
class. The author of C (different person) apparently knew it was new-style
and took advantage of that fact to use property(), but didn't notice there
was already an attribute named "attr" two levels up.

This of course all gets to the Zen bit: "Flat is better than nested", and
I'll use that as an argument for flatter hierarchies in the future.
Nonetheless, what would be the effect of such an attribute stomping? Do I
just have multiple ways to change self.attr, assuming __slots__ hasn't been
declared? This seems like something perhaps pychecker should notice, but
can it?

Thx,

Skip


I have run in this kind of troubles when working with Zope. Zope
hierarchies
as the best example of spaghetti-inheritance I have seen till now;
just today I
was looking at the CMF BaseFolder class: it has 33 ancestors (33!)
with definitions scattered in dozen of modules, packages and
subpackages,
and multiple inheritance abused at ridicoulous levels: how in they
hell they believe I can keep track of all the attributes defined in 33
ancestors??

So, I have written a routine to travel the MRO of the any class I
define;
the routine checks that I am not overriding accidentally an already
defined
attribute (which is pretty likely for what I am doing now and has
already bitten me and my coworkers a couple of times). Unfortunately
I cannot use metaclasses, so I can only get the shadowing attribute
warning
after the class creation and not before, and I have to invoke the
checking
function each time I define a new class (i.e. it is not automatic).

But anyway it helps a bit.
Michele Simionato
Jul 18 '05 #2

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

Similar topics

4
2039
by: DraguVaso | last post by:
Hi, I'm having some weird behaviour, and I can't find any solution to this. I made a UserControl (uclCommands) which contains a Toolbar (ToolBar1) with Modifiers = Public. I put this usercontrol on my form and call it Ucl1. In the Property Window in the Windows Forms Designer I change Ucl1.ToolBar1.ButtonSize to a bigger value.
18
4764
by: Robin Becker | last post by:
Is there a way to override a data property in the instance? Do I need to create another class with the property changed? -- Robin Becker
6
2746
by: Tom Kiefer | last post by:
Question: If I have an ASP.NET User Control which defines/exposes a property that the page can use to specify a mode or data subset for the control to use, is there a way to tell the @OutputCache directive to vary its cache based on that property value? I.e., I have: <my:control runat="server" id="mcOne" Flag="One" /> <my:control runat="server" id="mcTwo" Flag="Two" />
25
1762
by: Michal Kwiatkowski | last post by:
Hi, Code below shows that property() works only if you use it within a class. ------------------------------------------------ class A(object): pass a = A() a.y = 7
5
2315
by: Kent.Gallinger | last post by:
I have a web application that contains asp.net and classic asp pages. I have a vb.net function that I would like to use in one of the classic asp pages. How can I do this? Can I do this? Thanks, Kent.
7
1559
by: Mike Owen | last post by:
I have a vb.net page that uses a form as below: <form id="Visual" name="Visual" method="post" runat="server"> If I run this on my Windows XP development PC then the HTML generated from it is: <form name="Visual" method="post" action="Salesreport.aspx?Index=7" language="javascript" onsubmit="javascript:return WebForm_OnSubmit();" id="Visual">
11
3971
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject is instance of MyEntity class. There is no MyProperty property in MyObject at design time.
2
7202
by: funktacular | last post by:
Hi - I have some javascript that works when I run it from a server, but I need to run it locally. When I try to execute it locally I get the following error: Error: uncaught exception: Permission denied to get property Window.processXML Is there a way to get around this issue? default.htm
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.