define class in Function and how to use this class

A forum for general discussion of the Python programming language.

define class in Function and how to use this class

Postby vijaykumbhani » Wed Oct 16, 2013 5:08 am

Code: Select all
def myFunction(text):
     class MyClass():
            @classmethod
            def _get_kind(cls):
                   return text


how to access local class ?
Last edited by micseydel on Wed Oct 16, 2013 5:34 am, edited 2 times in total.
Reason: Code tags, OP lock.
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: define class in Function and how to use this class

Postby micseydel » Wed Oct 16, 2013 5:36 am

Why are you defining a class inside of a function? Why does that class have a class method? Both of those are features which are very rarely needed.

That said, your function can return the class, since everything is an object that you can manipulate.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1488
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: define class in Function and how to use this class

Postby Mekire » Wed Oct 16, 2013 5:38 am

Ok... so once again your question is not very clear, and once again you have not detailed any personal efforts to solve this problem.

But... why would you want to define a class inside a function anyway?

-Mek
User avatar
Mekire
 
Posts: 1120
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: define class in Function and how to use this class

Postby vijaykumbhani » Wed Oct 16, 2013 11:11 am

how to access local class outside function
ok, clear my questions
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: define class in Function and how to use this class

Postby Mekire » Wed Oct 16, 2013 11:51 am

Micseydel already answered that. If you absolutely must define a class within a function, you could return it. Classes are objects just like anything else.

There is however, almost. no. possible. way. that. you. need. to. do. this.

-Mek

Code: Select all
def return_a_class(arg):
    class LocalClass(object):
        def meth(self):
            print(arg)
    return LocalClass

if __name__ == "__main__":
    MyClass = return_a_class("Ow, the pain.")
    an_instance = MyClass()
    an_instance.meth()
User avatar
Mekire
 
Posts: 1120
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: define class in Function and how to use this class

Postby vijaykumbhani » Wed Oct 16, 2013 11:54 am

Thanks
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: define class in Function and how to use this class

Postby Kebap » Wed Oct 16, 2013 1:57 pm

Dear vijaykumbhani, please read this:

Describe the goal, not the step

If you are trying to find out how to do something (as opposed to reporting a bug), begin by describing the goal. Only then describe the particular step towards it that you are blocked on.

Often, people who need technical help have a high-level goal in mind and get stuck on what they think is one particular path towards the goal. They come for help with the step, but don't realize that the path is wrong. It can take substantial effort to get past this.

Stupid: How do I get the color-picker on the FooDraw program to take a hexadecimal RGB value?

Smart: I'm trying to replace the color table on an image with values of my choosing. Right now the only way I can see to do this is by editing each table slot, but I can't get FooDraw's color picker to take a hexadecimal RGB value.

The second version of the question is smart. It allows an answer that suggests a tool better suited to the task.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 402
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests