473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

question about class methods

I've run across some code in a class method that I don't understand:

def example(self, val=0)
if val and not self:
if self._exp < 0 and self._exp >= -6:

I am unfamiliar with some concepts here:

1) Under what circumstances would "if not self" be True?

2) If "not self" is True, how can self have attributes?

(This is slightly simplified code from the decimal.Decimal.__str__ method,
line 826 in python-2.4.4)

Thanks,
Darren
Mar 14 '07 #1
2 985
In <et**********@ruby.cit.cornell.edu>, Darren Dale wrote:
I've run across some code in a class method that I don't understand:

def example(self, val=0)
if val and not self:
if self._exp < 0 and self._exp >= -6:

I am unfamiliar with some concepts here:

1) Under what circumstances would "if not self" be True?
If an object is "true" is determined either by a `__nonzero__()` method
that returns `True` or `False` or a `__len__()` method where anything
except 0 means "true".
2) If "not self" is True, how can self have attributes?
Attributes are independent from the "truth" value of an object.

Ciao,
Marc 'BlackJack' Rintsch
Mar 14 '07 #2
En Wed, 14 Mar 2007 18:04:00 -0300, Darren Dale <dd**@cornell.edu>
escribió:
I've run across some code in a class method that I don't understand:

def example(self, val=0)
if val and not self:
if self._exp < 0 and self._exp >= -6:
0) "Normal" methods are not class methods, but instance methods. A class
method is a method who operates on the class itself; its first argument is
the class (maybe a derived one).
I am unfamiliar with some concepts here:

1) Under what circumstances would "if not self" be True?
"if not self" would be "if self evaluates to False as a boolean": 0, 0.0,
0j, (), [], {}... Other objects usually evaluate always to True, except if
they define some special methods: __nonzero__ and __len__. Look those on
the Python Reference Manual.
2) If "not self" is True, how can self have attributes?
Perhaps you think of self being None - that should never occur unless you
called the method in some convoluted way.
The class migh inherit from list, by example, and you want to compute the
average:

class StatList(list):
def avg(self):
if self: return sum(self)/len(self)
else: raise ValueError("Can't compute average on empty list")
(This is slightly simplified code from the decimal.Decimal.__str__
method,
line 826 in python-2.4.4)
A Decimal number is False when 0. `if not self` is a faster way to say `if
self==0`

--
Gabriel Genellina

Mar 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
7
by: Joe Fallon | last post by:
I have a WinForm that is a Base class. It has many controls on it and a lot of generic code behind. When I inherit the form I override 5 methods and now each child form has the same look and feel...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
23
by: neoedmund | last post by:
There's a program, it's result is "unexpected aaa", i want it to be "expected aaa". how to make it work? class C1(object): def v(self, o): return "expected "+o class C2(object):
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.