help with calling a static method in a private class

L

lallous

How can I keep the class private and have the following work:

Code:
class __internal_class(object):
    @staticmethod
    def meth1(s):
        print "meth1:", s

    @staticmethod
    def meth2(s):
        print "meth2:",
        __internal_class.meth1(s)

x = __internal_class()

x.meth2('sdf')

Thanks
 
D

Diez B. Roggisch

lallous said:
How can I keep the class private and have the following work:

Code:
class __internal_class(object):
@staticmethod
def meth1(s):
print "meth1:", s

@staticmethod
def meth2(s):
print "meth2:",
__internal_class.meth1(s)

x = __internal_class()

x.meth2('sdf')

By not using a double underscore. It is effectless on classes anyway
(they are not hidden because of that).

And additionally, but simply not using staticmethods at all. It's a
rather obscure feature of python - usually, classmethods are what is
considered a static method in other languages. And with that, even your
double underscores work:

class __internal_class(object):
@classmethod
def meth1(cls, s):
print "meth1:", s

@classmethod
def meth2(cls, s):
print "meth2:",
cls.meth1(s)

x = __internal_class()

x.meth2('sdf')


Diez
 
B

Bruno Desthuilliers

Diez B. Roggisch a écrit :
lallous said:
How can I keep the class private and have the following work:

Code:
class __internal_class(object):
@staticmethod
def meth1(s):
print "meth1:", s

@staticmethod
def meth2(s):
print "meth2:",
__internal_class.meth1(s)

x = __internal_class()

x.meth2('sdf')

By not using a double underscore. It is effectless on classes anyway
(they are not hidden because of that).

<OP>
FWIW, if what you want is to mark the class as being implementation (ie:
not part of your module's API), just prefix it with a single underscore.
And additionally, but simply not using staticmethods at all.

+1
 
L

lallous

lallous said:
How can I keep the class private and have the following work:
Code:
class __internal_class(object):
    @staticmethod
    def meth1(s):
        print "meth1:", s[/QUOTE]
[QUOTE]
    @staticmethod
    def meth2(s):
        print "meth2:",
        __internal_class.meth1(s)[/QUOTE]
[QUOTE]
x = __internal_class()[/QUOTE]
[QUOTE]
x.meth2('sdf')

By not using a double underscore. It is effectless on classes anyway
(they are not hidden because of that).

And additionally, but simply not using staticmethods at all. It's a
rather obscure feature ofpython- usually, classmethods are what is
considered a static method in other languages. And with that, even your
double underscores work:

class __internal_class(object):
    @classmethod
    def meth1(cls, s):
        print "meth1:", s

    @classmethod
    def meth2(cls, s):
        print "meth2:",
        cls.meth1(s)

x = __internal_class()

x.meth2('sdf')

Diez

Thanks, that does the trick.
 
S

Steven D'Aprano

And additionally, but simply not using staticmethods at all. It's a
rather obscure feature of python - usually, classmethods are what is
considered a static method in other languages.

Are you sure about that? I know Java isn't necessarily "other languages",
but my understanding is that static methods in Java are the same as
static methods in Python -- they're essentially ordinary functions glued
to a class, and they don't receive either the instance or the class as an
argument.

http://leepoint.net/notes-java/flow/methods/50static-methods.html


(Aside: I don't know about others, but I find that article *incredibly*
hard to read. E.g.

Static methods typically take all they data from
parameters and compute something from those parameters,
with no reference to variables.

What, parameters aren't variables? What about local variables? I know
what they *mean*, but it causes a double-take every time I read it. And
when they distinguish between *classes* and *objects*, that's another
double-take, because of course in Python classes are objects.)


C# seems to be the same:

http://dotnetperls.com/static-method

as is C++ (I believe), except I'm too lazy to find a good reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top