473,386 Members | 1,766 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,386 software developers and data experts.

``Type-checking'' with dir()

In order to leave my classes open to receiving objects that are
string-like, list-like, dictionary-like, etc, and not necessarily
_exactly_ the built-in string, list, dictionary, etc types, I have the
desire to just check if the necessary methods exist. Instead of:

if type(a) == type(""):
...

.. . . I like:

wanted_methods = ["__getslice__", "__len__"]
if len([ m
for m in dir(a)
if m in wanted_methods ]) == len(wanted_methods):
...

In the above example, I might want to get a slice of variable a if
it's length is appropriate. The above code is a simplified in-line
version of, say, has_methods(). Any thoughts?

Thanks!
Harry.
Jul 18 '05 #1
3 1498

"Harry Pehkonen" <ha************@hotpop.com> wrote in message
news:70**************************@posting.google.c om...
In order to leave my classes open to receiving objects that are
string-like, list-like, dictionary-like, etc, and not necessarily
_exactly_ the built-in string, list, dictionary, etc types, I have the
desire to just check if the necessary methods exist. Instead of:

if type(a) == type(""):
...

. . . I like:

wanted_methods = ["__getslice__", "__len__"]
if len([ m
for m in dir(a)
if m in wanted_methods ]) == len(wanted_methods):
...

In the above example, I might want to get a slice of variable a if
it's length is appropriate. The above code is a simplified in-line
version of, say, has_methods(). Any thoughts?

The code becomes especially clean if you use the sets module:

if not Set(["__getslice__", "__len__"]) < Set(dir(a)):
raise InterfaceError(a)
Raymond Hettinger
Jul 18 '05 #2
Raymond Hettinger wrote:
...
The code becomes especially clean if you use the sets module:

if not Set(["__getslice__", "__len__"]) < Set(dir(a)):
raise InterfaceError(a)


I think you want a <= here -- otherwise, an InterfaceError would
be raised in the (unlikely, sure;-) case in which dir(a) has
ONLY the __len__ and __getslice__ entries.

(BTW, of course: __getslice__ is obsolete, and properly substituted
by __getitem__ taking a slice object as an 'index' in today's
well-implemented sequences -- so, this is not a good example
[not Raymond's fault, of course, as it was what the OP used]).
Alex

Jul 18 '05 #3
oops, I meant "is going to be passed list-like objects etc ..."
Jul 18 '05 #4

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

Similar topics

3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
3
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.