Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104620

Re: Encapsulation in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Encapsulation in Python
Date Fri, 11 Mar 2016 08:08:45 -0700
Lines 20
Message-ID <mailman.23.1457708968.26429.python-list@python.org> (permalink)
References <56E17985.7060002@benmezger.nl> <87a8m5pdl6.fsf@handshake.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de AxzDk6NjRG6ywKpvXtYeuwWDjbRnjFECvltQKiZGPGbg==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.020
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:Python': 0.05; 'attributes': 0.07; 'caller': 0.07; 'python': 0.10; '2016': 0.16; 'constructs': 0.16; 'instance:': 0.16; 'metaclass': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'java': 0.22; 'enforce': 0.22; 'am,': 0.23; 'defined': 0.23; 'header:In-Reply-To:1': 0.24; 'define': 0.27; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; '(maybe': 0.29; 'declared': 0.29; 'class.': 0.30; 'though,': 0.32; 'getting': 0.33; 'class': 0.33; 'instances': 0.33; 'policies': 0.33; 'similar': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'i.e.': 0.35; 'instance': 0.35; 'level': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'anything': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'advanced': 0.61; 'real': 0.62; 'capable': 0.65; 'mar': 0.65; 'to:name:python': 0.84; 'subject:skip:E 10': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=2NGa3DTY/OwGMv1m838dohZ4rSPUOleswWSkKLlqq0w=; b=dwKslT2R0eiwIh9m04MMjnKKeqvNJ7jPqXRLb/T1eyLWCsoTQOW35DMwMCAG8uMIAj tshyrnCfhg0IhwV1uNGAAUDOn9qOglU9qRKQVD3+rKjzvYEkCRxqlkfBmAOv5eI9z/cy c0LDFnX9ciMo5QkU31Tt2gdPzz0N5TZblhKh0AXlD/o8g1Bv3hEr+foznDezzsG1cNW/ +dmnw+TE0mcKP4sxe4LOagPMHvqcYd2/8oy2TqkS+uBg731cPcVseS+WfCJ4NT86C95U 0W9c8PbakML7fCs+bxCCn5H0U0UTl+VEDmnc7R3z6l+3QPPc5mCv03p/mjptYFQ/EZZ/ fxbA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=2NGa3DTY/OwGMv1m838dohZ4rSPUOleswWSkKLlqq0w=; b=Zi0ckw++OCecX1Uw5QdsM3a7ZC8K1tHOKXzz1ilRUBPHfzVyMrupiyQtxIuYEZdSEu Ho/gw3x/uICbPGJGtf3bdu5JVRsc6YkxRdJGNu4d79kEdwfV/jpVuc+CYSxX02GhQch/ ZD5rd0ew416Ujg7WBf6sT5ouwsbdC8BF4o7vQRC+3C8JZ2dzF+A689UJBuv5aUM+wkNv J+9i3ZcObfwN7kddag1bSR1CYpViFb1OE5h7IcIvLD9DT0JOQbwnzWsWQaXaEtRSYmBg +AJwFUEA/YtBWr87SIwLjQznrPjlgJb90jFFZ4osmQvzlXu2SXLaT8peLvYxtbpQ407r MZoQ==
X-Gm-Message-State AD7BkJIUA1wWbMOnT3M00Tugs5h0gjbtel/sxzsTrFLMgzh4JDDZ3SKSu+Kwws0NkWAQBwRLXNg0q2jeOgLVeg==
X-Received by 10.50.112.10 with SMTP id im10mr4068980igb.93.1457708964786; Fri, 11 Mar 2016 07:09:24 -0800 (PST)
In-Reply-To <87a8m5pdl6.fsf@handshake.de>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104620

Show key headers only | View raw


On Fri, Mar 11, 2016 at 2:29 AM, dieter <dieter@handshake.de> wrote:
> If you are really interested to enforce Java encapsulation policies
> (access to attributes via "getter/setter" only), you will need
> to use your own "metaclass".
>
> The "metaclass" has a similar relation to a class as a class to
> an instance: i.e. it constructs a class. During the class construction,
> your "metaclass" could automatically define "getter/setter" methods
> for declared class attributes and hide the real attributes (maybe
> by prefixing with "__").
> Of course, class level (non-method) attributes are rare; most
> attributes of Python instances are not defined at the class level
> but directly at the instance level - and the metaclass would
> need to define "__setattr__" and "__getattribute__" to control access
> to them.

Pythonically, one would use a property to do this. You don't need
anything so advanced as a metaclass. Using either approach though,
there is no place you can hide the real attributes where the caller
isn't capable of getting at them.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-11 08:08 -0700

csiph-web