A newbie question about class
newgene
newgene at bigfoot.com
Sun Feb 10 12:03:11 EST 2002
"Mark McEahern" <marklists at mceahern.com> wrote in message news:<mailman.1013306655.4125.python-list at python.org>...
> [newgene]
> > Hi, group,
> > I have a newbie question about building a class:
> >
> > eg.
> >
> > class A:
> > def __init__(self,x=None):
> > self.x=x
> > if self.x != None:
> > self.y=x**2
> > else:
> > self.y=x**2
>
> 1. When comparing to None, it's better to use one of the following:
>
> if x is not None:...
> if x is None:...
Could you explain a little more why "is not" is better than "!="?
>
> 2. Consider using property, which is new with Python 2.2 and requires a
> new-style class (thus we subclass from object):
>
> class A(object):
>
> def __init__(self, x=None):
> self._x = x
>
> def getX(self):
> return self._x
>
> def setX(self, x):
> self._x = x
>
> x = property(getX, setX)
>
> a = A(1)
> print a.x
>
> Cheers,
>
> // mark
Thank you and other experts above. I got the solution for this problem now.
Newgene
More information about the Python-list
mailing list