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


Groups > comp.lang.python > #17010

Re: Working with Descriptors

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'assign': 0.04; 'instance,': 0.05; 'attribute': 0.07; 'attribute.': 0.09; 'descriptor': 0.09; 'descriptors': 0.09; 'def': 0.13; 'descriptor,': 0.16; 'failed)': 0.16; 'replaces': 0.16; 'cc:addr :python-list': 0.16; 'received:74.125.82.44': 0.16; 'received :mail-ww0-f44.google.com': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'dec': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'appear': 0.23; 'assigning': 0.23; 'here?': 0.23; 'cc:2**0': 0.24; 'work,': 0.26; "i'm": 0.26; 'import': 0.27; 'all,': 0.28; 'random': 0.28; '(this': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'object.': 0.30; 'second,': 0.30; 'sun,': 0.30; '\xa0\xa0\xa0': 0.31; 'that,': 0.33; 'there': 0.33; 'object': 0.33; 'received:74.125.82': 0.35; 'something': 0.35; 'subject:with': 0.36; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'couple': 0.38; 'created': 0.38; 'put': 0.38; 'first.': 0.39; 'either': 0.39; 'called': 0.40; '2011': 0.61; '11,': 0.68; 'directly.': 0.68; 'value):': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=7vRHvc8mOA1sqoUUx4bWnDqXotKgyt8ZNWVGguIqIDM=; b=DSubQcrzXrlqTKkQ4coq3SHyksfl6nNHUlbz/M5iGHS97zU1SORvg1VATkfP9xeteC 2NyU5RvNdEFc1xzA0LnpERstHE/ZRy08Ie17Jzfc1azqVmdFBkZQUdDUbzgihSqoVmrG mXMgva7KQBZhgNaJqwXAgx3lWJ6LtYvXwcxVs=
MIME-Version 1.0
In-Reply-To <CAOypoo5zbw8hVKvsmpUpCSMXm+hjvOP-aEJbffsU-KzVHLDH8Q@mail.gmail.com>
References <CAOypoo5zbw8hVKvsmpUpCSMXm+hjvOP-aEJbffsU-KzVHLDH8Q@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 11 Dec 2011 22:05:27 -0700
Subject Re: Working with Descriptors
To Emeka <emekamicro@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3529.1323666360.27778.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323666360 news.xs4all.nl 6948 [2001:888:2000:d::a6]:35594
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17010

Show key headers only | View raw


On Sun, Dec 11, 2011 at 9:32 PM, Emeka <emekamicro@gmail.com> wrote:
>
> Hello All,
>
> How do I get the __set__ to work here?
>
> import random
>
> class Die(object):
>     def __init__(self, sides=6):
>         self.sides = sides
>
>     def __get__(self, instance, owner):
>         return int(random.random() * self.sides) + 1
>
>     def __set__(self, instance, value):
>         instance.__dict__[self.side] = value
>
>
>
> class Game(object):
>     d6 = Die()
>     d10 = Die(sides=10)
>     d20 = Die(sides=20)
>
>
> Game.d3 = 90 (This failed)


I'm not sure exactly what it is you're trying to do with this, but
there are a couple problems.  First of all, at "Game.d3 = 90" you
appear to be trying to set the value of a descriptor on a class
object.  This doesn't work, because __set__ doesn't get called when
you try to set the descriptor on the class object directly.  It only
reassigns the attribute and replaces the descriptor.  You need to
either create an instance of Game and use the descriptors on that, or
put the descriptor in a metaclass.

Second, you're assigning to the d3 descriptor, but you never created a
descriptor for the d3 attribute.  The only descriptors on the Game
class are d6, d10, and d20.  If you're trying to assign to the
descriptor, it would need to exist first.  If you're trying to add a
new descriptor, you would need to do something like "Game.d3 =
Die(sides=3)".

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


Thread

Re: Working with Descriptors Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-11 22:05 -0700

csiph-web