The right way to 'call' a class attribute inside the same class

Thomas 'PointedEars' Lahn PointedEars at web.de
Mon Dec 12 15:59:40 EST 2016


Juan C. wrote:

> On Mon, Dec 12, 2016 at 12:34 PM, Thomas 'PointedEars' Lahn
> <PointedEars at web.de> wrote:
>> To call something means generally in programming, and in Python, to
>> execute it as a function instead: In the code above, the class object
>> referred to by “Box” is called twice in order to instantiate twice
>> (calling a class object in Python means to instantiate it, implicitly
>> calling its constructor method, “__init__”, if any), and the global
>> “print” function is called three times.
> 
> Since we are talking about Python terminology I believe that calling
> `__init__` a constructor is also wrong. I've already seem some
                                   ^^^^^
> discussions regarding it and the general consensus is that `__init__`
> shouldn't be called constructor as it isn't really a constructor (like
> Java/C# constructors). […]

IBTD:

,-<http://foldoc.org/constructor>
| 
| constructor
| 
| (programming)   1. In object-oriented languages, a function provided by a 
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^
| class to initialise a newly created object. The constructor function 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| typically has the same name as the class. It may take arguments, e.g. to 
| set various attributes of the object or it may just leave everything 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| undefined to be set elsewhere.
| 
| A class may also have a destructor function that is called when objects of 
| the class are destroyed.

Free On-line Dictionary of Computing, Last updated: 2014-10-04.

In Python, a class’s __init__() is called after its __new__() [1], and 
“typically” means “not always” (for another example, in clean PHP code the 
constructor’s name is “__construct”).

<https://docs.python.org/3/reference/datamodel.html?highlight=__init__#object.__init__>

[In fact, there are constructors in object-oriented programming languages 
with prototype-based inheritance, like implementations of ECMAScript Ed. 1 
to 7 (except 4), too.]

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.


More information about the Python-list mailing list