[Tutor] inquiry

Steven D'Aprano steve at pearwood.info
Sat Nov 26 07:34:43 EST 2016


Hi Phoenix, welcome!

On Sat, Nov 26, 2016 at 12:59:37PM +0800, zxjhust1 wrote:

> I have some questions about memory mechanism of python. Do the 
> elements of the parent go into the namespace of the subclass 
> when we define subclass?

I don't quite understand your question. I *think* you are asking if the 
parent's attributes are copied into the subclass. The answer to that is 
no, they are not:


py> class Parent:
...     member = 'leg'
...
py> class Child(Parent):
...     pass
...
py> print(Child.member)
leg
py> print('member' in vars(Child))
False


The vars() function returns the namespace of the argument, so there is 
no attribute "member" inside the Child namespace, instead the attribute 
is looked up when needed.


> As far as I'm concerned, they do go into the namespace of the 
> subclass. Because when we use dir(), we can see all the elements. But 
> there is only one 'update' name. I think the function of the subclass 
> override the parent's.  In this situation , when we instantiate the 
> subclass, we just need one argument, that is to say, the parent's 
> update function do the work. It confuses me.

Your question confuses me. What "update" name? Can you show a SMALL 
example that explains what you are talking about?

The dir() function shows attribute and method names of the object, its 
class, and its parent classes.

> According to my understanding, as there is no 'super' key word, the 
> 'self' should inference to the instance of the subclass when we 
> initiate the subclass. So self.__update should inference to function 
> of the subclass.

I don't know what you mean by "self.__update" here, but there is an 
added complication when the method name starts with two underscores: 
name mangling. The interpreter will insert the name of the class at the 
front of the method.

Name mangling is intended as a simple form of method protection, but in 
my experience it causes more problems than it solves. I'm happy to talk 
about name mangling more if you like, but as a beginner you will 
probably be much happier if you forget all about it until you have more 
experience.

There is no "super" keyword, but there is a super() function. But 
beware: in Python 2 it is complicated to get right.

Suppose I say:

class Animal:
    def grow(self):
        print(self, 'is growing')

class Mammal(Animal):
    pass

class Dog(Mammal):
    pass

lassie = Dog()  # an instance

lassie.grow()


then I get something like this:

<__main__.Dog object at 0xb79d0a4c> is growing

So it doesn't matter which class the method is actually defined in, the 
instance is still a Dog instance.


> Another quesion is about the meaning of the private variables. What 
> condition shall we use them? In other word, I wanna know the purpose 
> of introducing the private variables. The above-mentioned example, I 
> think, is not enough to explain it, because when we change the 
> __update to update, there is no change except for using update rather 
> than _Mapping__update to inference it.

In some languages, like Java, programmers make a big deal about private 
variables. The languages enforces them as private, and then programmers 
typically spend hours trying to fight the language to bypass the 
protection.

In Python, private variables are not enforced by the language. They are 
just a naming convention: if you name a method or attribute with a 
single leading underscore, like _update(), that is a sign to other 
people "Don't use this". It means that you reserve the right to remove 
the _update() method with no warning, or make it do something else.

In short, as a Python programmer using classes or functions from a 
library, you should NEVER use anything that starts with an underscore.

As a Python programmer, the only time you might use something with an 
underscore is if you are writing your own classes.


> The last question is about the storage of the data. For a given data, 
> like 1 or '1', what's the form when python saves it in the memory? To 
> be more specific, I wanna know the number of the parts we use to 
> enclose it. For example, python uses 3 parts to save it. One is for 
> the value. The second for the type of the data, ie, inter,str,etc. The 
> last is for the pointer or id to show the place where it is stored.

That depends on which Python interpreter you are using, and which 
version of Python.

But all values in Python are objects. How the object is defined in 
memory will depend on which interpreter, which version, and which 
object, but they would usually have at least two internal fields:

- the type of the object (an int, float, str, list, dict, etc)
- the value of the object (depends on what type it is).

You cannot directly access these fields from Python code. You can get 
the type of the object with the type() function:

py> type(lassie)  # remember this from before?
<class '__main__.Dog'>
py> type(42)
<class 'int'>
py> type("hello world")
<class 'str'>


Hope these answer your questions.


Regards,



Steve


More information about the Tutor mailing list