<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: No need for set/get methods in Python</title>
	<atom:link href="http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/</link>
	<description>Simple is better than complex. Complex is better than complicated. &#124; @fedecarg</description>
	<lastBuildDate>Mon, 08 Mar 2010 06:09:25 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: amanita</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-2191</link>
		<dc:creator>amanita</dc:creator>
		<pubDate>Mon, 26 Jan 2009 13:38:27 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-2191</guid>
		<description>Instead of 
Book.title = &#039;Code Complete&#039;
shouldn&#039;t write:
book = Book()
book.title = &#039;Code Complete&#039;

With:
book._title = &#039;foo&#039;
I can modify the property from the outside anyway ... right?</description>
		<content:encoded><![CDATA[<p>Instead of<br />
Book.title = &#8216;Code Complete&#8217;<br />
shouldn&#8217;t write:<br />
book = Book()<br />
book.title = &#8216;Code Complete&#8217;</p>
<p>With:<br />
book._title = &#8216;foo&#8217;<br />
I can modify the property from the outside anyway &#8230; right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nisse</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1867</link>
		<dc:creator>Nisse</dc:creator>
		<pubDate>Wed, 29 Oct 2008 11:20:55 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1867</guid>
		<description>Well, those both seem obsolete after seeing how it&#039;s done in ruby.

class Book
  attr_accessor :title
end

which gives you

book = Book.new
book.title = &#039;rftw&#039;
puts book.title # =&gt; &quot;rftw&quot;</description>
		<content:encoded><![CDATA[<p>Well, those both seem obsolete after seeing how it&#8217;s done in ruby.</p>
<p>class Book<br />
  attr_accessor :title<br />
end</p>
<p>which gives you</p>
<p>book = Book.new<br />
book.title = &#8216;rftw&#8217;<br />
puts book.title # =&gt; &#8220;rftw&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Federico</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1332</link>
		<dc:creator>Federico</dc:creator>
		<pubDate>Tue, 19 Aug 2008 13:43:50 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1332</guid>
		<description>Hi masklinn,

&gt; Don&#039;t use double underscore prefixes. Ever.
Changed. Thanks for the tip.

&gt; with Python 2.4 or more recent, you can use the &#039;property&#039; function 
Nice :)</description>
		<content:encoded><![CDATA[<p>Hi masklinn,</p>
<p>&gt; Don&#8217;t use double underscore prefixes. Ever.<br />
Changed. Thanks for the tip.</p>
<p>&gt; with Python 2.4 or more recent, you can use the &#8216;property&#8217; function<br />
Nice :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: masklinn</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1327</link>
		<dc:creator>masklinn</dc:creator>
		<pubDate>Tue, 19 Aug 2008 08:31:01 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1327</guid>
		<description>Another remark: with Python 2.4 or more recent, you can use the `property` function (because it&#039;s nothing more) as a decorator if you want e.g. to create a read-only property or make a member read-only:

&gt;&gt;&gt; class Foo(object):
....def __init__(self, some_value):
........self.some_value = some_value

		
&gt;&gt;&gt; foo = Foo(42)
&gt;&gt;&gt; foo.some_value
42
&gt;&gt;&gt; foo.some_value = 5
&gt;&gt;&gt; foo.some_value
5
&gt;&gt;&gt; class Foo(object):
....def __init__(self, some_value):
........self._some_value = some_value
....@property
....def some_value(self):
........return self._some_value

&gt;&gt;&gt; foo = Foo(42)
&gt;&gt;&gt; foo.some_value
42
&gt;&gt;&gt; foo.some_value = 5

Traceback (most recent call last):
..File &quot;&quot;, line 1, in 
....foo.some_value = 5
AttributeError: can&#039;t set attribute
&gt;&gt;&gt;</description>
		<content:encoded><![CDATA[<p>Another remark: with Python 2.4 or more recent, you can use the `property` function (because it&#8217;s nothing more) as a decorator if you want e.g. to create a read-only property or make a member read-only:</p>
<p>&gt;&gt;&gt; class Foo(object):<br />
&#8230;.def __init__(self, some_value):<br />
&#8230;&#8230;..self.some_value = some_value</p>
<p>&gt;&gt;&gt; foo = Foo(42)<br />
&gt;&gt;&gt; foo.some_value<br />
42<br />
&gt;&gt;&gt; foo.some_value = 5<br />
&gt;&gt;&gt; foo.some_value<br />
5<br />
&gt;&gt;&gt; class Foo(object):<br />
&#8230;.def __init__(self, some_value):<br />
&#8230;&#8230;..self._some_value = some_value<br />
&#8230;.@property<br />
&#8230;.def some_value(self):<br />
&#8230;&#8230;..return self._some_value</p>
<p>&gt;&gt;&gt; foo = Foo(42)<br />
&gt;&gt;&gt; foo.some_value<br />
42<br />
&gt;&gt;&gt; foo.some_value = 5</p>
<p>Traceback (most recent call last):<br />
..File &#8220;&#8221;, line 1, in<br />
&#8230;.foo.some_value = 5<br />
AttributeError: can&#8217;t set attribute<br />
&gt;&gt;&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: masklinn</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1326</link>
		<dc:creator>masklinn</dc:creator>
		<pubDate>Tue, 19 Aug 2008 08:23:08 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1326</guid>
		<description>Remarks from a pythonista:

* Don&#039;t use double underscore prefixes. Ever. It&#039;s bad style. A single underscore is all you need, don&#039;t go for more, name mangling wasn&#039;t introduced to create pseudo-private members.

* While I realize it&#039;s for the sake of the example, don&#039;t create empty properties (the ones you&#039;ve created here) either, they create indirection and make the code slower and more complex for no gain. You can transparently introduce properties later if and when you need them.</description>
		<content:encoded><![CDATA[<p>Remarks from a pythonista:</p>
<p>* Don&#8217;t use double underscore prefixes. Ever. It&#8217;s bad style. A single underscore is all you need, don&#8217;t go for more, name mangling wasn&#8217;t introduced to create pseudo-private members.</p>
<p>* While I realize it&#8217;s for the sake of the example, don&#8217;t create empty properties (the ones you&#8217;ve created here) either, they create indirection and make the code slower and more complex for no gain. You can transparently introduce properties later if and when you need them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1322</link>
		<dc:creator>Robert</dc:creator>
		<pubDate>Mon, 18 Aug 2008 19:56:46 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1322</guid>
		<description>I use a technique using __call() instead of __get() and __set(). propertys are get and set using the convention used for functions like session_id(), where the function called with no args will return the current value, and the property can be set by calling with the new value, ie $object-&gt;property() returns, $object-&gt;property(&quot;new value&quot;) sets.

the __call() function checks against a list of available variables, so principles of data hiding can be adhered to where required. 

this method has the added advantage of being able to overwrite this default behaviour by adding in any other function, as in the requirement above of adding in a new filter/modifier on a property.

the __call() function can be seen at http://pastebin.com/f28429455. the code is ripped out of my current DataObject class and has some additional code to allow all objects to return the objects ID and load status.</description>
		<content:encoded><![CDATA[<p>I use a technique using __call() instead of __get() and __set(). propertys are get and set using the convention used for functions like session_id(), where the function called with no args will return the current value, and the property can be set by calling with the new value, ie $object-&gt;property() returns, $object-&gt;property(&#8220;new value&#8221;) sets.</p>
<p>the __call() function checks against a list of available variables, so principles of data hiding can be adhered to where required. </p>
<p>this method has the added advantage of being able to overwrite this default behaviour by adding in any other function, as in the requirement above of adding in a new filter/modifier on a property.</p>
<p>the __call() function can be seen at <a href="http://pastebin.com/f28429455" rel="nofollow">http://pastebin.com/f28429455</a>. the code is ripped out of my current DataObject class and has some additional code to allow all objects to return the objects ID and load status.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Federico</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1321</link>
		<dc:creator>Federico</dc:creator>
		<pubDate>Mon, 18 Aug 2008 12:27:20 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1321</guid>
		<description>@Radoslav: Yes, that&#039;s a valid example. It&#039;s similar to the one I posted before. And that&#039;s exactly the point, to show how useful the property construct is.</description>
		<content:encoded><![CDATA[<p>@Radoslav: Yes, that&#8217;s a valid example. It&#8217;s similar to the one I posted before. And that&#8217;s exactly the point, to show how useful the property construct is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Radoslav Stankov</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1320</link>
		<dc:creator>Radoslav Stankov</dc:creator>
		<pubDate>Mon, 18 Aug 2008 12:11:45 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1320</guid>
		<description>may be something like this(a very dirty and slow, but will work)

http://codepad.org/75ZWvYI7</description>
		<content:encoded><![CDATA[<p>may be something like this(a very dirty and slow, but will work)</p>
<p><a href="http://codepad.org/75ZWvYI7" rel="nofollow">http://codepad.org/75ZWvYI7</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Federico</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1318</link>
		<dc:creator>Federico</dc:creator>
		<pubDate>Mon, 18 Aug 2008 11:49:37 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1318</guid>
		<description>@Sam: Yes, but what happens if a new variable is introduced, for example, author? Here&#039;s your example: http://codepad.org/3jj562He

1. You are converting all the values to lower-case. That&#039;s not part of the requirement.
2. The callers of the class are accessing an attribute that was never implemented: author.

An object should store its state in variables and expose its behaviour through methods, that&#039;s a fundamental principle in object-oriented programming.</description>
		<content:encoded><![CDATA[<p>@Sam: Yes, but what happens if a new variable is introduced, for example, author? Here&#8217;s your example: <a href="http://codepad.org/3jj562He" rel="nofollow">http://codepad.org/3jj562He</a></p>
<p>1. You are converting all the values to lower-case. That&#8217;s not part of the requirement.<br />
2. The callers of the class are accessing an attribute that was never implemented: author.</p>
<p>An object should store its state in variables and expose its behaviour through methods, that&#8217;s a fundamental principle in object-oriented programming.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/#comment-1317</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Mon, 18 Aug 2008 10:22:20 +0000</pubDate>
		<guid isPermaLink="false">http://phpimpact.wordpress.com/?p=654#comment-1317</guid>
		<description>class Book {
private $properties;

function __get($property) {
return strtolower($this-&gt;properties[$property]);
}

function __set($property, $value) {
$this-&gt;properties[$property] = $value;
}
}

$book = new Book();
$book-&gt;title = ‘Code Complete’;
echo $book-&gt;title;</description>
		<content:encoded><![CDATA[<p>class Book {<br />
private $properties;</p>
<p>function __get($property) {<br />
return strtolower($this-&gt;properties[$property]);<br />
}</p>
<p>function __set($property, $value) {<br />
$this-&gt;properties[$property] = $value;<br />
}<br />
}</p>
<p>$book = new Book();<br />
$book-&gt;title = ‘Code Complete’;<br />
echo $book-&gt;title;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
