[Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

boB Stepp robertvstepp at gmail.com
Sat Jan 23 02:52:26 EST 2016


On Fri, Jan 22, 2016 at 11:04 PM, Cameron Simpson <cs at zip.com.au> wrote:
> On 22Jan2016 22:14, boB Stepp <robertvstepp at gmail.com> wrote:

[...]

> Consider: this is information you want assicated with a specific object.
> Therefore it really _is_ an arribute of the object so that it can follow it
> around.
>
>> And what bothered me about my original example that started this
>> thread is that when my typo
>>
>> humdrum.sigh_strenght = 'high'
>>
>> was accepted and did not generate an error, it seemed to mean to me
>> that I was violating my object's data encapsulation.  It just seems to
>> me that I should not be able to arbitrarily add new attributes from
>> outside the class definition that created my object.  That seems
>> similar to having a class Dog, to which from outside the class'
>> definition, I decide to add a new Dog attribute that all dogs in this
>> class can now have a tail sticking out of their noses.  I have no
>> qualms about subclassing Dog with a MutantDog subclass, but adding new
>> attributes from outside the class definition?  That just does not seem
>> right to at this point of my understanding.  As in your function
>> example, I am sure that I am missing something quite obvious, but I am
>> just not seeing it now.
>
>
> All you're missing is realising that setting an attribute is not a special
> operation.

I guess no matter how new one is to OOP, one nevertheless brings one's
preconceptions, however malformed, into the learning process.  In my
case, one of mine was that once a class is coded, any given instance
of a class is forevermore responsible for managing its *internals*, so
that any change in these would be managed by the object when one of
its (meant to be) publicly available methods is called.  In the case
of attributes, I was thinking/expecting something like the following
would have to be done:

>>> class Dog(object):
        def __init__(self, name, breed):
            self.name = name
            self.breed = breed
            self.number_legs = 4
            self.number_tails = 1
            self.number_eyes = 2
            self.attributes = {
                'name': self.name,
                'breed': self.breed,
                'number of legs': self.number_legs,
                'number of tails': self.number_tails,
                'number of eyes': self.number_eyes}

        def add_attribute(self, new_attribute_name, new_attribute_value):
            self.new_attribute_name = new_attribute_name
            self.new_attribute_value = new_attribute_value
            self.attributes[self.new_attribute_name] = self.new_attribute_value

        def show_attributes(self):
            print("The object,", self.name, "has the following attributes:")
            for attribute_name, attribute_value in self.attributes.items():
                print(attribute_name, "=", attribute_value)

>>> our_dog = Dog('Copper', 'beagle')
>>> our_dog.show_attributes()
The object, Copper has the following attributes:
number of tails = 1
number of legs = 4
breed = beagle
name = Copper
number of eyes = 2
>>> our_dog.add_attribute('unique marking', 'blue right eye')
>>> our_dog.show_attributes()
The object, Copper has the following attributes:
number of legs = 4
breed = beagle
unique marking = blue right eye
number of tails = 1
name = Copper
number of eyes = 2

I suspect there are probably better ways to do what I am trying to
demonstrate [I am trying to use a dict to simulate adding new instance
variables after the fact.], perhaps with built-in functions that I
have not read about yet.  But I hope my code illustrates my previous
expectations.  I guess I was expecting that access to an object's
internals would be more strictly regulated.  But, thinking about it, I
already knew that even if name mangling is done for a method's name,
it can still be directly accessed from outside of that object's class
definition.

But if I am understanding everyone's comments correctly, Python allows
me write my classes in such a way, that public access to my object's
internals is controlled, in the sense that Python has coding
conventions that tell all of the quite intelligent, respectful,
consenting adults that might use my code how I intend that code to be
used.  And that I, despite my intentions, must respect that users of
my code may have very valid reasons for deviating from my intentions.
Have I captured the Pythonic spirit of what everyone has been trying
to tell me?

Thanks for your patience, now and in the future, as I try to learn OOP
in Python!

Cheers!
boB


More information about the Tutor mailing list