a dummy problem on static method

This is the place for queries that don't fit in any of the other categories.

a dummy problem on static method

Postby tianchen » Fri Dec 27, 2013 2:39 pm

hi everyone:

Can I ask a dummy question here?
I have a class named Selector and a static method f1 which calls a "private" function with double underscore named __f2 inside, but it returns error "global name '_Selector__f1' is not defined",when I use it in the main function in this way

Code: Select all
Selector.f1()

class Selector:
       @staticmethod
       def f1(*para):
             variable=__f2(given parameter)

       def__f2(*para):


I have no idea what is wrong. Can you give me a hand? Thanks!!!
Last edited by metulburr on Fri Dec 27, 2013 3:02 pm, edited 1 time in total.
Reason: locked and added code tags
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: a dummy problem on static method

Postby Yoriz » Fri Dec 27, 2013 3:11 pm

The code you have posted will give various other errors but probably not the error you have stated, could you post the proper code and the full error traceback
Please read the 'new users, read this' link in my signature for advice on how to post code and ask questions.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 764
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: a dummy problem on static method

Postby metulburr » Fri Dec 27, 2013 3:16 pm

you have a lot of typos in the given code. Plus you did not give a useable piece of code with such arguments. Make sure you have that when posting on forums.

I think this is accurate. I am not sure though because i never use instance-less classes. You have to have Selelctor.__f2(arg)
Code: Select all
class Selector:
    @staticmethod
    def f1(*para):
        variable = Selector.__f2(para)

    def __f2(*para):
        pass
           

Selector.f1()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: a dummy problem on static method

Postby Mekire » Fri Dec 27, 2013 3:34 pm

The static method doesn't know what class it is called from and can't find the function.

You could do weird stuff like this:
Code: Select all
class Selector(object):
    @staticmethod
    def f1(var):
        return Selector.__f2(var)

    @staticmethod
    def __f2(var):
        print(var*5)


Selector.f1(45)

Or goofy stuff like this:
Code: Select all
class Selector(object):
    @classmethod
    def f1(cls,var):
        return cls.__f2(var)

    @staticmethod
    def __f2(var):
        print(var*5)


Selector.f1(45)

But, honestly you probably shouldn't be doing either (using static methods, class methods, or name mangling variables for that matter).

-Mek

Edit @Metul:
Your versions would work in python 3 but in python 2 you would get this unless you also declared __f2 a static method:
Code: Select all
TypeError: unbound method __f2() must be called with Selector instance as first argument (got tuple instance instead)
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: a dummy problem on static method

Postby tianchen » Fri Dec 27, 2013 4:05 pm

Yoriz wrote:The code you have posted will give various other errors but probably not the error you have stated, could you post the proper code and the full error traceback
Please read the 'new users, read this' link in my signature for advice on how to post code and ask questions.


Thanks for the reminding, Yoriz. I will take care of this next time I post a question.
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: a dummy problem on static method

Postby tianchen » Fri Dec 27, 2013 4:24 pm

metulburr wrote:you have a lot of typos in the given code. Plus you did not give a useable piece of code with such arguments. Make sure you have that when posting on forums.

I think this is accurate. I am not sure though because i never use instance-less classes. You have to have Selelctor.__f2(arg)
Code: Select all
class Selector:
    @staticmethod
    def f1(*para):
        variable = Selector.__f2(para)

    def __f2(*para):
        pass
           

Selector.f1()


Thank you for your comments, metulburr:)
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: a dummy problem on static method

Postby tianchen » Fri Dec 27, 2013 4:28 pm

Mekire wrote:The static method doesn't know what class it is called from and can't find the function.

You could do weird stuff like this:
Code: Select all
class Selector(object):
    @staticmethod
    def f1(var):
        return Selector.__f2(var)

    @staticmethod
    def __f2(var):
        print(var*5)


Selector.f1(45)

Or goofy stuff like this:
Code: Select all
class Selector(object):
    @classmethod
    def f1(cls,var):
        return cls.__f2(var)

    @staticmethod
    def __f2(var):
        print(var*5)


Selector.f1(45)

But, honestly you probably shouldn't be doing either (using static methods, class methods, or name mangling variables for that matter).

-Mek

Edit @Metul:
Your versions would work in python 3 but in python 2 you would get this unless you also declared __f2 a static method:
Code: Select all
TypeError: unbound method __f2() must be called with Selector instance as first argument (got tuple instance instead)


Thank you Mekire, the problem is solved. Can I ask one step further, what is the difference between class method and static method? As I know they are both methods bound to class instead of a instance. Thank you for your concise explanation again.
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: a dummy problem on static method

Postby Yoriz » Fri Dec 27, 2013 4:33 pm

New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 764
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: a dummy problem on static method

Postby micseydel » Fri Dec 27, 2013 9:11 pm

In Python we rarely have static methods; they're common in Java because functions are not first-class. There is not need for them in Python, and although they sometimes make sense 99.9% of the time a function is better.

Class methods are only useful when you need the cls variable, which again, is very rare.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1180
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: a dummy problem on static method

Postby tianchen » Sat Dec 28, 2013 9:14 pm

micseydel wrote:In Python we rarely have static methods; they're common in Java because functions are not first-class. There is not need for them in Python, and although they sometimes make sense 99.9% of the time a function is better.

Class methods are only useful when you need the cls variable, which again, is very rare.


Learned. Thank you very much for your time.
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests