Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

EDIT: responding to comment

Not sure I have a firm handle on descriptors, but the example in the docs access value with self rather than invoking getattr() and setattr().

However, this answer this answer and this answer this answer do not user getattr(), they do not use self either. They access the use the instance argument.

My example is slightly simpler. What are the trade-offs?

Just as in the original question, the b instance demonstrates that a ValueError is raised. I added a worth attribute simply to demonstrate that there are in fact two distinct instances of NumberDescriptor.

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

EDIT: responding to comment

Not sure I have a firm handle on descriptors, but the example in the docs access value with self rather than invoking getattr() and setattr().

However, this answer and this answer do not user getattr(), they do not use self either. They access the use the instance argument.

My example is slightly simpler. What are the trade-offs?

Just as in the original question, the b instance demonstrates that a ValueError is raised. I added a worth attribute simply to demonstrate that there are in fact two distinct instances of NumberDescriptor.

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

EDIT: responding to comment

Not sure I have a firm handle on descriptors, but the example in the docs access value with self rather than invoking getattr() and setattr().

However, this answer and this answer do not user getattr(), they do not use self either. They access the use the instance argument.

My example is slightly simpler. What are the trade-offs?

Just as in the original question, the b instance demonstrates that a ValueError is raised. I added a worth attribute simply to demonstrate that there are in fact two distinct instances of NumberDescriptor.

Responding to comment
Source Link

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

EDIT: responding to comment

Not sure I have a firm handle on descriptors, but the example in the docs access value with self rather than invoking getattr() and setattr().

However, this answer and this answer do not user getattr(), they do not use self either. They access the use the instance argument.

My example is slightly simpler. What are the trade-offs?

Just as in the original question, the b instance demonstrates that a ValueError is raised. I added a worth attribute simply to demonstrate that there are in fact two distinct instances of NumberDescriptor.

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age

EDIT: responding to comment

Not sure I have a firm handle on descriptors, but the example in the docs access value with self rather than invoking getattr() and setattr().

However, this answer and this answer do not user getattr(), they do not use self either. They access the use the instance argument.

My example is slightly simpler. What are the trade-offs?

Just as in the original question, the b instance demonstrates that a ValueError is raised. I added a worth attribute simply to demonstrate that there are in fact two distinct instances of NumberDescriptor.

Source Link

Good question. I'm studying descriptors too.

Why do you use setattr() and getattr()? I'm not clear on why you store age inside instance. What benefit does this have over this naive example?

class NumberDescriptor(object):
 def __set__(self, instance, value):
 if not isinstance(value, int):
 raise ValueError("%s is not a number." % value)
 else:
 self.value = value
 def __get__(self, instance, owner):
 return self.value
class Human(object):
 age = NumberDescriptor()
 worth = NumberDescriptor()
 def __init__(self,age,worth):
 self.age = age
 self.worth = worth
a = Human(12,5)
print a.age, a.worth
a.age = 13
a.worth = 6
print a.age, a.worth
b = Human("osman", 5.0)
print b.age
lang-py

AltStyle によって変換されたページ (->オリジナル) /