[Tutor] Class learning

Danny Yoo danny.yoo at gmail.com
Fri Jan 23 11:49:21 CET 2015


You are trying to use advanced features of Python, and they are not
the right tool for what you're trying to do.

Specifically, you're trying two things at the same time:

1.  Properties, which allows method calls to look like simple variable access.

2.  The __name__ special attribute on methods (reference:
https://docs.python.org/2/reference/datamodel.html) to reflectively
pick up a string that lets us get the name of a function.


The problem is trying to use *both* of these features at the same
time.  It is self defeating.


Here is a minimal example to demonstrate;

##################
class Test(object):
    @property
    def x(self):
        return 42
##################

Consider the expression:

    Test2().x.__name__

This example is small enough that it should help to clarify what's
going on.  What did you want to happen?  And what happens?



Now look at:

#####################
class Test(object):
    def x(self):
        return 42

Test().x().__name__
#####################

What do you expect to see when you run this, and why?


The technical error in the first case is the same as the second.


In short, I would strongly suggest you don't use @property, especially
if you're learning the language.  It's an advanced feature.  In your
particular case, you're getting into unnecessary trouble by using it.


More information about the Tutor mailing list