Python Forum
Calling a class from a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a class from a function
#1
Hi all,

I'm pretty noob in python hence the question. Blush
Is it possible to call a class from a function?

Cheers,
Reply
#2
I'm not sure if this is what you mean, but yes it is possible
>>> class A:
...     pass
... 
>>> def func():
...     a = A()
...     print(a)
... 
>>> func()
<__main__.A object at 0x7f8e9a4da430>
jc4d likes this post
Reply
#3
(Dec-15-2021, 08:26 AM)Gribouillis Wrote: I'm not sure if this is what you mean, but yes it is possible
>>> class A:
...     pass
... 
>>> def func():
...     a = A()
...     print(a)
... 
>>> func()
<__main__.A object at 0x7f8e9a4da430>

Thank you, I noticed you have a pass statement at the end of the class, is that necessary? I ask because my classes have a return {'FINISHED'}

I will test it out on my messy script to see if it can hold it Smile
Reply
#4
jc4d Wrote:I noticed you have a pass statement at the end of the class, is that necessary?
No it is not necessary. I wrote the 'pass' in order to give a non empty body to the class definition. A better idea is to write a docstring
>>> class A:
...     """This class does nothing"""
... 
>>> 
The class body can only contain return statements in the method's definition.
Reply
#5
(Dec-15-2021, 10:51 AM)Gribouillis Wrote: No it is not necessary. I wrote the 'pass' in order to give a non empty body to the class definition.
Thank you for the explanation. I will do some test now on my script.
Reply
#6
You don't explain what you mean when you say "call a class", since that's not standard terminology. In any case, if you generally mean using a class in a function, what makes you think you couldn't do that?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 605 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 7 1,421 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 807 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,034 Sep-19-2022, 02:32 AM
Last Post: Xeno
  TimeOut a function in a class ? Armandito 1 1,637 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
Sad Iterate randint() multiple times when calling a function Jake123 2 2,037 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,667 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,746 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  a function common to methods of a class Skaperen 7 2,565 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Tuple generator, and function/class syntax quazirfan 3 3,852 Aug-10-2021, 09:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020