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


Groups > comp.lang.python > #21696

Questions about the use of descriptors.

Path csiph.com!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 <steveo@syslang.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.037
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'attribute': 0.07; 'e.g.,': 0.07; 'descriptor': 0.09; 'descriptors': 0.09; 'question?': 0.09; 'to:name:python list': 0.09; 'def': 0.13; 'descriptors.': 0.16; 'fruit': 0.16; 'hypothetical': 0.16; 'obj,': 0.16; 'val': 0.16; 'examples': 0.16; 'instance': 0.18; 'subject:Questions': 0.18; "doesn't": 0.22; 'frequent': 0.23; 'subject:use': 0.24; 'classes': 0.26; 'variable': 0.28; 'bit': 0.28; "i'm": 0.28; 'fine.': 0.29; 'class': 0.29; 'example': 0.29; 'print': 0.29; 'seem': 0.29; 'zero.': 0.30; 'does': 0.32; 'this.': 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'to:addr:python-list': 0.35; 'none': 0.35; 'things': 0.35; 'question': 0.36; 'class.': 0.37; 'created': 0.37; 'but': 0.37; 'using': 0.37; 'another': 0.37; 'skip:_ 10': 0.38; 'uses': 0.38; 'getting': 0.38; 'should': 0.38; "i'd": 0.39; 'change': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'simple': 0.61; 'your': 0.61; 'property': 0.63; 'note:': 0.69; 'divided': 0.73; 'god': 0.79; "driver's": 0.84; 'flies': 0.84; 'me!': 0.84; 'stranger': 0.84; 'holes': 0.91; '000': 0.93
Date Thu, 15 Mar 2012 13:32:14 -0400
From "Steven W. Orr" <steveo@syslang.net>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20
MIME-Version 1.0
To python list <python-list@python.org>
Subject Questions about the use of descriptors.
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.689.1331832740.3037.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331832740 news.xs4all.nl 6857 [2001:888:2000:d::a6]:60522
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21696

Show key headers only | View raw


Question 1:

I have a class A with one attribute and I define __get__ and __set__ for that 
class. Then I create another class B that uses it.

Why does B require that the instance of A be a class variable in B and not 
created as an instance variable in __init__?

E.g.,
# This works fine.
class Truth(object):
     def __init__(self):
         self.is_slave = False

     def __get__(self, obj, objtype):
         return self.is_slave

     def __set__(self, obj, val):
         if not self.is_slave and val:
             self.is_slave = val


class TruthHolder(object):
     IsSlave = Truth()

     def set_truth(self):
         self.IsSlave = True

tt = TruthHolder()
print tt.IsSlave
tt.IsSlave = True
print tt.IsSlave
tt.IsSlave = False
print tt.IsSlave


But if I change TruthHolder to not start as a class variable

class TruthHolder(object):
     def __init__(self):
         self.IsSlave = Truth()

     def set_truth(self):
         self.IsSlave = True

it doesn't seem to use descriptor methods of Truth. It's just using the 
default setter and getter of TruthHolder.

Question2:

Is it the case that people only use descriptors for classes with single 
attributes? Or is it more frequent that descriptors are used with classes that 
have multiple attributes?

I feel like this is powerful juju, but I'm not getting when I should be using 
property and when I should be using descriptors.

General note: I see really simple examples in my searches, but I'd like to see 
a good practical example that has just a bit more meat on it.

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

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


Thread

Questions about the use of descriptors. "Steven W. Orr" <steveo@syslang.net> - 2012-03-15 13:32 -0400

csiph-web