Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Questions about the use of descriptors.

Reply
Thread Tools

Questions about the use of descriptors.

 
 
Steven W. Orr
Guest
Posts: n/a
 
      03-15-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Few Questions (HW questions already answered by me) padh.ayo@gmail.com C Programming 10 12-06-2006 05:48 PM
Malloc and free questions - learner questions pkirk25 C Programming 50 10-04-2006 02:22 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments