<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Pimping Python&#8217;s property()</title>
	<atom:link href="http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/feed/" rel="self" type="application/rss+xml" />
	<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/</link>
	<description>branching out</description>
	<lastBuildDate>Tue, 17 Aug 2010 14:19:07 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jay</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-96</link>
		<dc:creator>Jay</dc:creator>
		<pubDate>Fri, 07 Dec 2007 05:11:18 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-96</guid>
		<description>Phil:

I ran into that problem a long time ago.  It&#039;s quite a Python wart.  I don&#039;t remember all the details, but basically the property is bound to the class it was created in and isn&#039;t overriden for you automatically.  Here&#039;s the work-around:

class Foo(object):
    def get_foo(self):
        return &#039;foo&#039;

    foo = property(lambda self: self.get_foo())

class Bar(Foo):
    def get_foo(self):
        return &#039;bar&#039;

print Foo().foo, Bar().foo

This will print &quot;foo bar&quot; instead of &quot;foo foo&quot; if you used property as you usually do.  All we do now is delay the evaluation of self until the property is actually called, so it picks up the correct subclass instead of being bound forever to the class it was instantiated in.
        return &#039;bar&#039;</description>
		<content:encoded><![CDATA[<p>Phil:</p>
<p>I ran into that problem a long time ago.  It&#8217;s quite a Python wart.  I don&#8217;t remember all the details, but basically the property is bound to the class it was created in and isn&#8217;t overriden for you automatically.  Here&#8217;s the work-around:</p>
<p>class Foo(object):<br />
    def get_foo(self):<br />
        return &#8216;foo&#8217;</p>
<p>    foo = property(lambda self: self.get_foo())</p>
<p>class Bar(Foo):<br />
    def get_foo(self):<br />
        return &#8216;bar&#8217;</p>
<p>print Foo().foo, Bar().foo</p>
<p>This will print &#8220;foo bar&#8221; instead of &#8220;foo foo&#8221; if you used property as you usually do.  All we do now is delay the evaluation of self until the property is actually called, so it picks up the correct subclass instead of being bound forever to the class it was instantiated in.<br />
        return &#8216;bar&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl Friedrich Bolz</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-95</link>
		<dc:creator>Carl Friedrich Bolz</dc:creator>
		<pubDate>Fri, 07 Dec 2007 02:00:20 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-95</guid>
		<description>Somewhat relevant blog post from Phillip J. Eby: http://dirtsimple.org/2004/12/python-is-not-java.html</description>
		<content:encoded><![CDATA[<p>Somewhat relevant blog post from Phillip J. Eby: <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="nofollow">http://dirtsimple.org/2004/12/python-is-not-java.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-94</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Thu, 06 Dec 2007 22:08:35 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-94</guid>
		<description>I have had some troubles with properties and inheritance. If I redefine a setter in a subclass, it&#039;s not called correctly unless the property() is redefined for the overriden setter. Same goes for getter... :(</description>
		<content:encoded><![CDATA[<p>I have had some troubles with properties and inheritance. If I redefine a setter in a subclass, it&#8217;s not called correctly unless the property() is redefined for the overriden setter. Same goes for getter&#8230; :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-93</link>
		<dc:creator>Jim</dc:creator>
		<pubDate>Thu, 06 Dec 2007 19:36:30 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-93</guid>
		<description>Actually, Beginning Python from Novice to Professional by Magnus Lie Hetland, covers this topic on pages 184-188.  Just thought you&#039;d like to know. 

No idea if Learning Python covers this or not.</description>
		<content:encoded><![CDATA[<p>Actually, Beginning Python from Novice to Professional by Magnus Lie Hetland, covers this topic on pages 184-188.  Just thought you&#8217;d like to know. </p>
<p>No idea if Learning Python covers this or not.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeff</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-91</link>
		<dc:creator>jeff</dc:creator>
		<pubDate>Thu, 06 Dec 2007 17:21:15 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-91</guid>
		<description>Ben and Eli: I understand the double underscore to work in the way that Eli described. In general, name conventions do help (not just to avoid name conflicts  but also for the programmer) but they can&#039;t enforce privacy. As you can tell, Ben, I came to Python from Java so I still learn things all the time about the pythonic way of doing things. =)

Tic-Tacs: from what I&#039;ve seen, __getattr__/__setattr__ seems like a much more popular way; I was wondering whether it was actually preferred or whether people generally just don&#039;t know about property(). The potential issue I can see with __getattr__/__setattr__ is that the bodies can get cluttered if you&#039;re trying to preserve an older interface with lots of types that have changed.

Thanks for the interesting replies so far! I love hearing other people&#039;s thoughts on this.</description>
		<content:encoded><![CDATA[<p>Ben and Eli: I understand the double underscore to work in the way that Eli described. In general, name conventions do help (not just to avoid name conflicts  but also for the programmer) but they can&#8217;t enforce privacy. As you can tell, Ben, I came to Python from Java so I still learn things all the time about the pythonic way of doing things. =)</p>
<p>Tic-Tacs: from what I&#8217;ve seen, __getattr__/__setattr__ seems like a much more popular way; I was wondering whether it was actually preferred or whether people generally just don&#8217;t know about property(). The potential issue I can see with __getattr__/__setattr__ is that the bodies can get cluttered if you&#8217;re trying to preserve an older interface with lots of types that have changed.</p>
<p>Thanks for the interesting replies so far! I love hearing other people&#8217;s thoughts on this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eli Courtwright</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-90</link>
		<dc:creator>Eli Courtwright</dc:creator>
		<pubDate>Thu, 06 Dec 2007 15:38:05 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-90</guid>
		<description>&quot;Actually if you prefix member data with a double underscore it becomes effectively private - but don’t do that.&quot;

Actually it doesn&#039;t become private; __bar in class Foo is simply changed to _Foo__bar.  This is not for encapsulation, but instead to prevent conflicts between classes and subclasses which both want fields with the same name which are not intended for public use.  If you define &quot;foo = Foo()&quot; then you&#039;ll still be able to say &quot;foo._Foo__bar&quot;.</description>
		<content:encoded><![CDATA[<p>&#8220;Actually if you prefix member data with a double underscore it becomes effectively private &#8211; but don’t do that.&#8221;</p>
<p>Actually it doesn&#8217;t become private; __bar in class Foo is simply changed to _Foo__bar.  This is not for encapsulation, but instead to prevent conflicts between classes and subclasses which both want fields with the same name which are not intended for public use.  If you define &#8220;foo = Foo()&#8221; then you&#8217;ll still be able to say &#8220;foo._Foo__bar&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tac-Tics</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-89</link>
		<dc:creator>Tac-Tics</dc:creator>
		<pubDate>Thu, 06 Dec 2007 15:20:31 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-89</guid>
		<description>Getters and setters are pointless boilerplate. A good language whose aim was strong encapsulation would allow for some sort of declarations to automatically generate getters and setters, since 99.99..% of the time, they&#039;re gonna be &quot;getBlah() {return blah; }&quot; and &quot;setBlah(stuff) { blah = stuff; }&quot;. Only in cases where implementations change (which really isn&#039;t frequent at all for most kinda of applications), you should have the ability to swap out the default getters/setters for some function. I would go so far as to say that automatically created getter/setter functions should be the default, and customer overloaded ones should require the declaration.

But for the half of the world who doesn&#039;t suck at programming, discipline and respect for public interface allows us to keep ourselves very content with Python. And really, accessing members directly isn&#039;t a bad thing in Python, because if you *do* need to change it to a custom getter or setter, you can just add a __getattr__/__setattr__ and get on with you life. 

(Python is great)</description>
		<content:encoded><![CDATA[<p>Getters and setters are pointless boilerplate. A good language whose aim was strong encapsulation would allow for some sort of declarations to automatically generate getters and setters, since 99.99..% of the time, they&#8217;re gonna be &#8220;getBlah() {return blah; }&#8221; and &#8220;setBlah(stuff) { blah = stuff; }&#8221;. Only in cases where implementations change (which really isn&#8217;t frequent at all for most kinda of applications), you should have the ability to swap out the default getters/setters for some function. I would go so far as to say that automatically created getter/setter functions should be the default, and customer overloaded ones should require the declaration.</p>
<p>But for the half of the world who doesn&#8217;t suck at programming, discipline and respect for public interface allows us to keep ourselves very content with Python. And really, accessing members directly isn&#8217;t a bad thing in Python, because if you *do* need to change it to a custom getter or setter, you can just add a __getattr__/__setattr__ and get on with you life. </p>
<p>(Python is great)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Scherrey</title>
		<link>http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/comment-page-1/#comment-88</link>
		<dc:creator>Ben Scherrey</dc:creator>
		<pubDate>Thu, 06 Dec 2007 12:42:21 +0000</pubDate>
		<guid isPermaLink="false">http://codefork.com/blog/index.php/2007/12/05/pimping-pythons-property/#comment-88</guid>
		<description>Actually if you prefix member data with a double underscore it becomes effectively private - but don&#039;t do that. :) As you will note, that combined with the property capability does provide a superior enforced encapsulation mechanism than java (and pretty much equivalent to C++). However, its rarely used in python and normally seen in code that is written by people who come to python via java or C++.

Frankly in a dynamically typed language its simply not needed because changing the internal type of the data member often does not break most public usage of the class. (Good unit tests will confirm this for you.) You will typically find the (proper) use of properties whenever you have a design-by-contract concept where the class method needs to check and guarantee certain variants. Just like real life, this is generally the exception rather than the rule and getters and setters turn out to be a lot of useless extra code and a &quot;Bad Idea&quot; TM. 

So before going nuts with properties, just try accessing the data directly. Its ok - no one will make fun of you. Its a very &quot;pythonic&quot; thing to do. Then, on those rare situations where you do need more enforcement by your code - it will be very clear to other programmers that this matters because that property will stand out rather than just be one property amongst the many.

Keep your code very short, obvious, and elegant. I came to Python via C++ (and still love C++ which it seems most python folk do not) and it took me a while to abandon all that extra code which, while necessary in C++ - in python, didn&#039;t really deliver me any extra business value and the compiler/environment and my project really would never use it. Things are different in a strong statically typed environment. Lose the extra class code and invest in unit tests instead. You&#039;ll be much happier. But its good to know about properties cause they are priceless when you really need them...

  Good luck!</description>
		<content:encoded><![CDATA[<p>Actually if you prefix member data with a double underscore it becomes effectively private &#8211; but don&#8217;t do that. :) As you will note, that combined with the property capability does provide a superior enforced encapsulation mechanism than java (and pretty much equivalent to C++). However, its rarely used in python and normally seen in code that is written by people who come to python via java or C++.</p>
<p>Frankly in a dynamically typed language its simply not needed because changing the internal type of the data member often does not break most public usage of the class. (Good unit tests will confirm this for you.) You will typically find the (proper) use of properties whenever you have a design-by-contract concept where the class method needs to check and guarantee certain variants. Just like real life, this is generally the exception rather than the rule and getters and setters turn out to be a lot of useless extra code and a &#8220;Bad Idea&#8221; TM. </p>
<p>So before going nuts with properties, just try accessing the data directly. Its ok &#8211; no one will make fun of you. Its a very &#8220;pythonic&#8221; thing to do. Then, on those rare situations where you do need more enforcement by your code &#8211; it will be very clear to other programmers that this matters because that property will stand out rather than just be one property amongst the many.</p>
<p>Keep your code very short, obvious, and elegant. I came to Python via C++ (and still love C++ which it seems most python folk do not) and it took me a while to abandon all that extra code which, while necessary in C++ &#8211; in python, didn&#8217;t really deliver me any extra business value and the compiler/environment and my project really would never use it. Things are different in a strong statically typed environment. Lose the extra class code and invest in unit tests instead. You&#8217;ll be much happier. But its good to know about properties cause they are priceless when you really need them&#8230;</p>
<p>  Good luck!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
