Is this object counter code pythonic

J

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
 
F

Fredrik Lundh

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>
 
J

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
 
D

Diez B. Roggisch

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
 
D

Duncan Booth

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.
 
B

bruno at modulix

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top