-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exercise 5.4 Need help with eliminating names from typedproperty #35
Comments
I'm also curious to see how to bind set_name function with property object. Any hint or solution will be appreciated |
class MyClass:
obj = MyClass() |
Is discussing solutions ok in this thread? Spoiler warning for my solutionUnsure if this is the intended solution or I've missed something which may cause a bug later on. I tried to override the The next attempt consisted of subclassing from property: class special_property(property):
def __set_name__(self, cls, name):
self.public_name = name
self.private_name = '_' + name
super().__set_name__(cls, name) The instance, def typedproperty(expected_type):
@special_property
def value(self):
return getattr(self, value.private_name)
@value.setter
def value(self, val):
if not isinstance(val, expected_type):
raise TypeError(f'Expected {expected_type}')
setattr(self, value.private_name, val)
return value |
Thanks, jrmylow. After commenting out this line " |
To answer an earlier question, I'm okay with solution discussion here. I never provided a solution to this (or any hints really). However, the only mechanism that Python provides to learn the name is One thing that is a little unsettling in this exercise is the idea that you could dynamically create a property inside a function and return it back as an object like this. Python lets you create anything you want inside a function and return it back. This has some application later when the course gets into metaprogramming. |
Hint: use set_name inside the typedproperty function so that it can access the name of the attribute that it's assigned to. Spoiler alert
|
This is my solution: def typedproperty(expected_type):
private_name = None
class NamedProperty(property):
def __set_name__(self, cls, name):
nonlocal private_name
private_name = '_' + name
def _get(self):
return getattr(self, private_name)
def _set(self, val):
if not isinstance(val, expected_type):
raise TypeError(f"Expected {expected_type}")
setattr(self, private_name, val)
return NamedProperty(_get, _set) |
In the last challenge of this exercise, we are asked to eliminate the need for providing names to
typedproperty
. I can't get this to work with the closure template provided. Can I get some more hints or just the solution, as I have been banging my head against this problem for a couple of hours now? I realize that__set_name__
is called under the hood when assigning properties to the class, and this was pretty clear in the valiidate.py exercise. I'm unsure how this works when extending the class with thesetypedproperty
methods. Thanks for the help.The text was updated successfully, but these errors were encountered: