Mailing List Archive

Question from a "java background" developer
Hello to everyone, I have a question. I come from a Java background and I
would like to develop in python but i'm wondering: is there anything, in
python, like Java "reflection"?
I mean do i have a keyword to obtain all the methods and the attributes of
a class in python?
Thanks to anyone that can answer me.
Best
Agnese
--
https://mail.python.org/mailman/listinfo/python-list
Re: Question from a "java background" developer [ In reply to ]
On Tue, Sep 22, 2020 at 7:15 PM Agnese Camellini
<agnese.camellini@gmail.com> wrote:
>
> Hello to everyone, I have a question. I come from a Java background and I
> would like to develop in python but i'm wondering: is there anything, in
> python, like Java "reflection"?
> I mean do i have a keyword to obtain all the methods and the attributes of
> a class in python?

Yes - introspection can be done by looking at a class's dictionary.
Check out the dir() function to start exploring!

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Question from a "java background" developer [ In reply to ]
On 9/22/20 3:25 AM, Chris Angelico wrote:
> On Tue, Sep 22, 2020 at 7:15 PM Agnese Camellini
> <agnese.camellini@gmail.com> wrote:
>>
>> Hello to everyone, I have a question. I come from a Java background and I
>> would like to develop in python but i'm wondering: is there anything, in
>> python, like Java "reflection"?
>> I mean do i have a keyword to obtain all the methods and the attributes of
>> a class in python?
>
> Yes - introspection can be done by looking at a class's dictionary.
> Check out the dir() function to start exploring!

Without knowing any specifics of what you're looking for you can examine
and modifiy object attributes. Python's dynamic nature makes the
modifying part easy (sometimes I think *too* easy :) ). Along with
dir(), other functions to read a bit on:

type, isinstance, callable, setattr, getattr as well as the attribute
__dict__.




--
https://mail.python.org/mailman/listinfo/python-list
Re: Question from a "java background" developer [ In reply to ]
Chris Angelico wrote at 2020-9-22 19:25 +1000:
>On Tue, Sep 22, 2020 at 7:15 PM Agnese Camellini
><agnese.camellini@gmail.com> wrote:
>>
>> Hello to everyone, I have a question. I come from a Java background and I
>> would like to develop in python but i'm wondering: is there anything, in
>> python, like Java "reflection"?
>> I mean do i have a keyword to obtain all the methods and the attributes of
>> a class in python?
>
>Yes - introspection can be done by looking at a class's dictionary.
>Check out the dir() function to start exploring!

I find also `help(obj)` very helpful.

The basics behind `help` (a built in function) come from
the `inspect` module. It is very helpful to programmatically inspect
features of Python objects.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Question from a "java background" developer [ In reply to ]
On Tue, Sep 22, 2020 at 11:13:50AM +0200, Agnese Camellini wrote:
> I mean do i have a keyword to obtain all the methods and the
> attributes of
> a class in python?

In addition to the `dir()` that others have mentioned, I'll add that
developing interactively is very common, especially in ipython/jupyter
sessions or notebooks. With these, you have tab completion or question
mark documentation.

For example, if you have an instance `myobj` of the class `MyClass`,
then in an ipython session, typing `myobj.<tab>` will open up a list of
all the methods for objects of `MyClass`. And given an object or
funcation, appending `?` will bring up the documentation (as given in
the docstring) for that object or function. This is true even for
user-defined functions and objects, even if they were defined during the
same interactive session.

Good luck!

--
David Lowry-Duda <david@lowryduda.com> <davidlowryduda.com>
--
https://mail.python.org/mailman/listinfo/python-list