473,578 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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).__nam e__ == '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.method Name)==???

Can someone help me?

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

iD8DBQFIu606cZ7 0OCIgLecRAhE6AJ 4r0GuHlWxXbLaYu olqpJStYPD+ggCg idKg
qtgl+nbaKgH5Aoe lTu5WeJU=
=W4eG
-----END PGP SIGNATURE-----
Sep 1 '08 #1
6 1044
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).__nam e__ == '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).__nam e__
'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.cybersourc e.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).__na me__
'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.me thodName)

Alternatively, you can read the docs:

help(obj)
help(obj.method Name)

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.instancemet hod)

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.instancemet hod)

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
20294
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object...
7
5820
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 brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member...
1
1300
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 member and not as temporary object (in vb.net its mean, how can i know the my object created as private \public memeber and not as dim )
4
2482
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
1698
by: nukiboy | last post by:
========= test.js =================== function Test() { this.temp = "hehehehe" ; this.method1 = method1 ;
3
1382
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 way. In the constructor i have created the connection instant in the following way
6
1191
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 objects Initialise non-shared member objects Initialise peripherals The places where these could take place would appear to be
11
3814
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
37
3919
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 a function
5
2381
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 object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
0
7850
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7779
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6524
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3786
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2293
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1391
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1114
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.