Is this object counter code pythonic

2006-04-10 Thread jnair
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this object counter code pythonic

2006-04-10 Thread Fredrik Lundh
[EMAIL PROTECTED] 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 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this object counter code pythonic

2006-04-10 Thread jnair
Ok got it .
Thanks a Lot

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this object counter code pythonic

2006-04-10 Thread jnair
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this object counter code pythonic

2006-04-10 Thread Diez B. Roggisch
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this object counter code pythonic

2006-04-10 Thread Duncan Booth
[EMAIL PROTECTED] 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.
-- 
http://mail.python.org/mailman/listinfo/python-list