473,385 Members | 1,848 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.

Re: Know if a object member is a method

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luca,

use type(something).__name__ , e.g.
>>def x():
pass
class C:
pass
c = C()
type(x).__name__ == 'function'
True
>type(C).__name__ == 'classobj'
True
>type(c).__name__ == 'instance'
True

On Sep 1, 2008, at 10:43 AM, Luca wrote:
Hi all.

I think this is a newbie question... what is the best method to know
if a property of an object is a function?

I'm thinking something as

if type(obj.methodName)==???

Can someone help me?

--
-- luca
--
http://mail.python.org/mailman/listinfo/python-list
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFIu606cZ70OCIgLecRAhE6AJ4r0GuHlWxXbLaYuolqpJ StYPD+ggCgidKg
qtgl+nbaKgH5AoelTu5WeJU=
=W4eG
-----END PGP SIGNATURE-----
Sep 1 '08 #1
6 1038
On Mon, 01 Sep 2008 10:52:10 +0200, Manuel Ebert wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Luca,

use type(something).__name__ , e.g.
>>def x():
>>> pass
>>class C:
>>> pass
>>c = C()
>>type(x).__name__ == 'function'
True
>type(C).__name__ == 'classobj'
True
>type(c).__name__ == 'instance'
True

That's relatively fragile, since such names aren't reserved in any way.
It's easy to fool a name comparison check with an accidental name
collision:
>>class function(object): # not a reserved name
.... pass
....
>>x = function()
type(x).__name__
'function'
>>x() # must be a function then...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'function' object is not callable
But not so easy to fool a type check:
>>type(x) == new.function
False

Of course that's not bullet-proof either. I leave it as an exercise to
discover how you might break that piece of code.

--
Steven
Sep 1 '08 #2
On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
<st****@remove.this.cybersource.com.auwrote:
That's relatively fragile, since such names aren't reserved in any way.
It's easy to fool a name comparison check with an accidental name
collision:
>>>class function(object): # not a reserved name
... pass
...
>>>x = function()
type(x).__name__
'function'
>>>x() # must be a function then...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'function' object is not callable
But not so easy to fool a type check:
>>>type(x) == new.function
False

Of course that's not bullet-proof either. I leave it as an exercise to
discover how you might break that piece of code.
Ok, so...

What is the best way to do this? The "most pythonic"?
--
-- luca
Sep 1 '08 #3
On Mon, 01 Sep 2008 11:45:36 +0200, Luca wrote:
>But not so easy to fool a type check:
>>>>type(x) == new.function
False

Of course that's not bullet-proof either. I leave it as an exercise to
discover how you might break that piece of code.

Ok, so...

What is the best way to do this? The "most pythonic"?
The "most pythonic" might be not checking at all but simply *call* the
object and deal with possible failures.

What's your use case?

Ciao,
Marc 'BlackJack' Rintsch
Sep 1 '08 #4
On Mon, 01 Sep 2008 11:45:36 +0200, Luca asked about recognizing methods:
What is the best way to do this? The "most pythonic"?
That depends on why you are doing it, and what you want to do with the
information once you've found it.

If you are experimenting in the interactive interpreter, the easiest way
is simply call the method and see what happens:

obj.methodName() # call the method

If it succeeds, then it is some sort of callable, a method or a function
or something more unusual.

If you fear side-effects, then use:

callable(obj.methodName)

Alternatively, you can read the docs:

help(obj)
help(obj.methodName)

If your aim is to write something like a debugger, profiler, or some
other application that needs to inspect arbitrary objects and work out
what they do, then you probably should be using:

isinstance(obj.methodName, new.instancemethod)

But remember that not all callable attributes are instancemethods!

There is no one right way of doing this.

Hope this helps.

--
Steven
Sep 1 '08 #5
On Mon, Sep 1, 2008 at 2:18 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
>
If your aim is to write something like a debugger, profiler, or some
other application that needs to inspect arbitrary objects and work out
what they do, then you probably should be using:

isinstance(obj.methodName, new.instancemethod)

But remember that not all callable attributes are instancemethods!

There is no one right way of doing this.

Hope this helps.
Yes, this helps a lot. In facts I need to do something like a language parser.

Thanks all.

--
-- luca
Sep 1 '08 #6
Luca wrote:
Yes, this helps a lot. In facts I need to do something like a language parser.
a *parser* that works on object structures created by executing a Python
program?

</F>

Sep 1 '08 #7

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
1
by: roni | last post by:
my target is to create objects only as data member of class. (but offcourse i can pass the objects reference to other objects) my question: how can i make sure that my object is create as data...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
5
by: nukiboy | last post by:
========= test.js =================== function Test() { this.temp = "hehehehe" ; this.method1 = method1 ;
3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
6
by: Charles Law | last post by:
As a matter of practice, where would people put the following elements of object creation/initialisation: Create shared member objects Initialise shared member objects Create non-shared member...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.