Greg,

Yes, what I describe may not be common and some code goes to serious lengths 
precisely to make direct connections to internals on an object hard to access.

But Python indeed allows and perhaps encourages you to use what you consider
side effects but perhaps more. There are many dunder methods that allow
all sorts of side effects or direct effects.

You can set it up so an access of a variable increments a counter
but perhaps it can set it up so three tries at guessing a password disables it 
or refuses
to accept a number out of your bounds. Or it may be that some accesses to say 
write
a value might also update several other values. For example, if you store a 
distance and
also a time, any change of either one resets the value of a stored item holding 
a velocity.
Perhaps adding additional values (or removing any) results in an immediate 
recalculation
of the mean or standard deviation or whatever.

If you allow direct access then you can make changes that get around that and I 
see no reason
to assume programs do not use such ideas.

Perhaps worse is that items may be dynamic. If an object representing me and my 
library card
checks out a book, a new object may be created that holds the book. If you copy 
a pointer to
the reference to the book in the library card object and later the book is 
returned and a new book
checked out, then your reference is now pointing to the old book. If your goal 
is to get to the
CURRENT book, you failed!

So I stand by my suggestion that unless you know the code well and can be sure 
you won't
corrupt things, this kind of convenient access has possible dangers.



-----Original Message-----
From: Greg Ewing <greg.ew...@canterbury.ac.nz>
To: python-list@python.org
Sent: Tue, Mar 22, 2022 7:12 pm
Subject: Re: convenience


On 23/03/22 7:00 am, Avi Gross wrote:

> But are there costs or even errors

> if you approach an inner part of an object directly? Can there be dunder 
> methods not

> invoked that would be from the standard approach? What kind of inadvertent

> errors can creep in?



The attribute could be a property that returns different objects

at different times depending on some condition. In that case

you will need to think about whether you want the current value

of the attribute, or the value it had when you looked it up

before.



Concerning dunder methods, there are a bunch of them involved in

looking up an attribute of an object. Theoretically it's possible

for these to have side effects and for bad things to happen if you

skip them. But that would be very unusual and surprising, and we

generally assume such things don't happen.



-- 

Greg

-- 

https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to