-1

In some code I found the statement:

p = property

and my question is what is the meaning intention of this statenmment? (The code I posted is an excerpt from a larger package (http://asymptote.sourceforge.net/), what is the general idea behind the p = propery?)

Some background, the complete file with this statement:

#!/usr/bin/env python3

import gettext

p = property

class xasyString:
    def __init__(self, lang=None):
        s = self
        if lang is None:
            _ = lambda x:  x
        else:
            lng = gettext.translation('base', localedir='GUI/locale', languages=[lang])
            lng.install()
            _ = lng.gettext

        s.rotate = _('Rotate')
        s.scale = _('Scale')
        s.translate = _('Translate')

        s.fileOpenFailed = _('File Opening Failed.')
        s.fileOpenFailedText = _('File could not be opened.')
        s.asyfyComplete = _('Ready.')

The only further reference I could find regarding p is:

class asyLabel(asyObj):
    """A python wrapper for an asy label"""
...
    def updateCode(self, asy2psmap=identity()):
        """Generate the code describing the label"""
        newLoc = asy2psmap.inverted() * self.location
        locStr = xu.tuple2StrWOspaces(newLoc)
        self.asyCode = 'Label("{0}",{1},p={2}{4},align={3})'.format(self.text, locStr, self.pen.getCode(), self.align,
        self.getFontSizeText())
albert
  • 8,285
  • 3
  • 19
  • 32
  • 9
    It doesn't do anything at all in the code you have posted. – Daniel Roseman Aug 05 '19 at 12:06
  • The code I posted is an excerpt from a larger package (http://asymptote.sourceforge.net/), what is the general idea behind the `p = propery`? – albert Aug 05 '19 at 12:08
  • 2
    Presumably, there are many uses of `foo = p(...)` or `@p def foo(...)` elsewhere in the code, indicating that the original author didn't want to type `property` many times. – chepner Aug 05 '19 at 12:09
  • @chepner so it is an way for "lazy typing person" more or less "comparable" the the C "#define p property". – albert Aug 05 '19 at 12:12
  • @albert Basically. It's generally a bad idea, because it optimizes for writability at the expense of readability (though there are exceptions: `import numpy as np` seems to be an accepted, widespread convention), and any decent text editor can simplify typing `property`. – chepner Aug 05 '19 at 12:16
  • @chepner I don't like abbreviations like these either as they make code unreadable but I found it in an external package (and as not a real Python programmer I was wondering what it means). – albert Aug 05 '19 at 12:17
  • Based on How does the https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work and the remark from @DanielRoseman shouldn't the assignment have some arguments? – albert Aug 05 '19 at 12:19
  • @albert No; `property` is a type (and it is callable, like every other type). `p` is being assigned the type `property` itself, not an *instance* of `property`. – chepner Aug 05 '19 at 13:24

1 Answers1

2

p = property simply makes p another reference to the property type, so that one can later write

@p
def foo(...):
    ...

instead of

@property
def foo(...):
    ...

property is a type, so p is just another name for the same type.

>>> type(property)
<type 'type'>
>>> p = property
>>> type(p)
<type 'type'>
>>> p is property
True
chepner
  • 497,756
  • 71
  • 530
  • 681
  • So the "lazy programmer / typer" option. As a side note in the mentioned code I didn't find any `@p` either. – albert Aug 05 '19 at 12:22