469,271 Members | 1,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Is this object counter code pythonic

My Team Lead says my object counter code seen below is not pythonic

class E(object):
_count = 0
def __init__(self):
E._count += 1
count = property(lambda self: E._count )

def test():
if __name__ == "__main__":
e1 = E()
print e1.count
e2 = E()
print e2.count
e3 = E()
print e3.count

test()

if not what would be the pythonic way

Apr 10 '06 #1
6 1057
jn***@ensim.com wrote:
My Team Lead says my object counter code seen below is not pythonic

class E(object):
_count = 0
def __init__(self):
E._count += 1
count = property(lambda self: E._count )

def test():
if __name__ == "__main__":
e1 = E()
print e1.count
e2 = E()
print e2.count
e3 = E()
print e3.count

test()

if not what would be the pythonic way
why are you using a getter (the property) instead of just exposing the
attribute ? why not just do

class E(object):
count = 0 # instance counter
def __init__(self):
E.count += 1

?

and this is a bit backwards:
def test():
if __name__ == "__main__":
/code/
test()


I suspect you meant to write:

if __name__ == "__main__":
def test():
/code/
test()

or even

if __name__ == "__main__":
/code/

</F>

Apr 10 '06 #2
Ok got it .
Thanks a Lot

Apr 10 '06 #3
Fredrik is then this a valid "property" use case and pythonic to
get/set a common varibale across objects

class E(object):
_i = 0
def geti(self) : return E._i
def seti(self,val) : E._i = val
i = property(geti,seti)

if __name__ == "__main__":
e1 = E()
e1.i = 100
e2 = E()
print e2.i

Apr 10 '06 #4
jn***@ensim.com wrote:
Fredrik is then this a valid "property" use case and pythonic to
get/set a common varibale across objects


No. you do that only if you have some kind of behavior attached - e.g. if
there are database queries to be made for returning a property or something
like that.

Diez
Apr 10 '06 #5
jn***@ensim.com wrote:
Fredrik is then this a valid "property" use case and pythonic to
get/set a common varibale across objects


A valid poperty use case would be one where you did something that couldn't
be done without using a property.

In some other languages you cannot simply change a public attribute into a
property, so you have to decide up front whether an attribute might ever
need to become a property. Since most programmers have at best only an
imperfect knowledge of the future, advice for those programming in such
languages is to never make attributes public.

Python isn't like that: if it needs to be a property, because you must do
something which special on either setting or accessing the value, then use
a property. If you don't need a property today, then just use an attribute.
You can change it into a property tomorrow when you find it needs to be a
property.
Apr 10 '06 #6
jn***@ensim.com wrote:
Fredrik is then this a valid "property" use case and pythonic to
get/set a common varibale across objects

class E(object):
_i = 0
def geti(self) : return E._i
def seti(self,val) : E._i = val
i = property(geti,seti)

if __name__ == "__main__":
e1 = E()
e1.i = 100
e2 = E()
print e2.i


Why do you want/think you need to hide the fact that i is an attribute
of class E ?

Actually, I find this use of properties very misleading:

e1.i = 42
e2.i = 1138
assert e1.i == 42, "WTF ???"
While this is perfectly obvious:

E.i = 42

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
4 posts views Thread by lauch2 | last post: by
7 posts views Thread by Carsten H. Pedersen | last post: by
4 posts views Thread by Carl J. Van Arsdall | last post: by
reply views Thread by freshman | last post: by
16 posts views Thread by king kikapu | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.