is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Tony Cappellini-2

Chapter 9, Program 4 is a simple GUI with 1 button and 1 text box, to illustrate a simple MVC pattern.

code is here -> https://pastebin.com/S35ZVW7k

The derived Model class originally used a staticmethod decorator for the fibonacci() function,
but the book doesn't discuss why. (The regular Python docs really don't describe staticmethod very clearly, either)

When I changed the decorated function to a class method, the program works
the same, from what I can see.

I'm curious what value the decorator was supposed to add, in this case.


Thanks

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Cody Precord
Hi,

Still subscribed by have mostly moved on to other things.

The @staticmethod decorator mostly just changes the calling semantics of a method decorated with it to remove the need to have an instance of the object to call the method.

Example:

class Foo:
   def instanceBar(self, x):
       # do stuff
   @staticmethod
   def staticBar(x):
       # do stuff

In order to call instanceBar you must have an instance of Foo which is implicitly passed to instnanceBar to satisfy the 'self' parameter.

i = Foo()
i.instanceBar(5)

However the static version does not require the instance to be declared so you can just execute the method by referencing the class type definition as there is no need to implicitly pass the instance reference to 'self' (cause there is no self.

Foo.staticBar(5);

Of course for a method to do this it must not rely on any memory that belongs to an instance of the containing class as it does not have access to any instance memory.


If your familiar with any other languages that have static access classifiers C++, C#, Java, ect...) the decorator provides similar functionality. 



Cody



On Wed, May 10, 2017 at 1:12 AM, cappy2112 <[hidden email]> wrote:

Chapter 9, Program 4 is a simple GUI with 1 button and 1 text box, to illustrate a simple MVC pattern.


The derived Model class originally used a staticmethod decorator for the fibonacci() function,
but the book doesn't discuss why. (The regular Python docs really don't describe staticmethod very clearly, either)

When I changed the decorated function to a class method, the program works
the same, from what I can see.

I'm curious what value the decorator was supposed to add, in this case.


Thanks

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Chris Barker - NOAA Federal
not about wxPython per se, but....

what Cody said.

And:

A "calssmethod" is a method with the first parameter the class object rather than an instance object, so:

@classmethod
def foo_class(cls, arg):
    ....

foo_class() can be called on Either an istance or a class object (though teh usual thing is to call it on a class object, and then "cls" will be the class object.

In your case, if you don't use "self" or "cls", than a regular method will work the same as a staticmethod or a classmethod, excep tthat staticmethod and classmethod can be called directly on the class object, without crating an instance.

If you call a regular method on a class object, you will get a UnboundMethodError, or something like that.

-CHB



On Tue, May 9, 2017 at 11:27 PM, Cody <[hidden email]> wrote:
Hi,

Still subscribed by have mostly moved on to other things.

The @staticmethod decorator mostly just changes the calling semantics of a method decorated with it to remove the need to have an instance of the object to call the method.

Example:

class Foo:
   def instanceBar(self, x):
       # do stuff
   @staticmethod
   def staticBar(x):
       # do stuff

In order to call instanceBar you must have an instance of Foo which is implicitly passed to instnanceBar to satisfy the 'self' parameter.

i = Foo()
i.instanceBar(5)

However the static version does not require the instance to be declared so you can just execute the method by referencing the class type definition as there is no need to implicitly pass the instance reference to 'self' (cause there is no self.

Foo.staticBar(5);

Of course for a method to do this it must not rely on any memory that belongs to an instance of the containing class as it does not have access to any instance memory.


If your familiar with any other languages that have static access classifiers C++, C#, Java, ect...) the decorator provides similar functionality. 



Cody



On Wed, May 10, 2017 at 1:12 AM, cappy2112 <[hidden email]> wrote:

Chapter 9, Program 4 is a simple GUI with 1 button and 1 text box, to illustrate a simple MVC pattern.


The derived Model class originally used a staticmethod decorator for the fibonacci() function,
but the book doesn't discuss why. (The regular Python docs really don't describe staticmethod very clearly, either)

When I changed the decorated function to a class method, the program works
the same, from what I can see.

I'm curious what value the decorator was supposed to add, in this case.


Thanks

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Tony Cappellini-2
Thanks Chris and Cody.
If a function can be called without the Le and instance, then what value is their having it in the class at all?
This looks like a friend in C++, which just tears the OO walls apart.
Why should a "callable"be part of a class , if it can be called without reference to the instance ?



On Wed, May 10, 2017 at 21:07 Chris Barker <[hidden email]> wrote:
not about wxPython per se, but....

what Cody said.

And:

A "calssmethod" is a method with the first parameter the class object rather than an instance object, so:

@classmethod
def foo_class(cls, arg):
    ....

foo_class() can be called on Either an istance or a class object (though teh usual thing is to call it on a class object, and then "cls" will be the class object.

In your case, if you don't use "self" or "cls", than a regular method will work the same as a staticmethod or a classmethod, excep tthat staticmethod and classmethod can be called directly on the class object, without crating an instance.

If you call a regular method on a class object, you will get a UnboundMethodError, or something like that.

-CHB



On Tue, May 9, 2017 at 11:27 PM, Cody <[hidden email]> wrote:
Hi,

Still subscribed by have mostly moved on to other things.

The @staticmethod decorator mostly just changes the calling semantics of a method decorated with it to remove the need to have an instance of the object to call the method.

Example:

class Foo:
   def instanceBar(self, x):
       # do stuff
   @staticmethod
   def staticBar(x):
       # do stuff

In order to call instanceBar you must have an instance of Foo which is implicitly passed to instnanceBar to satisfy the 'self' parameter.

i = Foo()
i.instanceBar(5)

However the static version does not require the instance to be declared so you can just execute the method by referencing the class type definition as there is no need to implicitly pass the instance reference to 'self' (cause there is no self.

Foo.staticBar(5);

Of course for a method to do this it must not rely on any memory that belongs to an instance of the containing class as it does not have access to any instance memory.


If your familiar with any other languages that have static access classifiers C++, C#, Java, ect...) the decorator provides similar functionality. 



Cody



On Wed, May 10, 2017 at 1:12 AM, cappy2112 <[hidden email]> wrote:

Chapter 9, Program 4 is a simple GUI with 1 button and 1 text box, to illustrate a simple MVC pattern.


The derived Model class originally used a staticmethod decorator for the fibonacci() function,
but the book doesn't discuss why. (The regular Python docs really don't describe staticmethod very clearly, either)

When I changed the decorated function to a class method, the program works
the same, from what I can see.

I'm curious what value the decorator was supposed to add, in this case.


Thanks

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to a topic in the Google Groups "wxPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wxpython-users/OAInMcKm6oI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Tim Roberts
Tony Cappellini wrote:

If a function can be called without the Le and instance, then what value is their having it in the class at all?
This looks like a friend in C++, which just tears the OO walls apart.

It's not like "friend" at all.  It's closer to a C++ static method.  The @classmethod is declared within the class, and has access to the class variables.  A class is an object, just like instances are objects:
C:\Users\timr>type x.py

class Foo(object):
    # A class member variable:
    count = 0

    def __init__(self):
        Foo.count += 1

    @classmethod
    def getcount(cls):
        return cls.count

x = Foo()
y = Foo()
z = Foo()
print( x.getcount() )
print( Foo.getcount() )

C:\Users\timr>python x.py
3
3

C:\Users\timr>

Why should a "callable"be part of a class , if it can be called without reference to the instance ?

Encapsulation.  If the method is working with Foo or Foo objects, then it should be part of the Foo class.
-- 
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Chris Barker - NOAA Federal
On Thu, May 11, 2017 at 10:05 AM, Tim Roberts <[hidden email]> wrote:
If a function can be called without the Le and instance, then what value is their having it in the class at all?
This looks like a friend in C++, which just tears the OO walls apart.

It's not like "friend" at all.  It's closer to a C++ static method.  The @classmethod is declared within the class, and has access to the class variables.

and it HAS to be part of the class, so you know what class object to pass in...

But I think the OP was asking about @staticmethod -- which really is jsut like a plain old stand alone function.

(not idea what "friend" is in C++)

So yes, any staticmthod could jstu be a stand alone functinin a module. But:

Other, more "pure" OO languages have satic methods because there is no such ting as a stand alone function. That doesn't apply to Python, but staticmethods allow you to put things that logically belong together together, and also allow a cleaner namespace:

from a_module import A_Class

A_Class.a_staticmethod1()

A_Class.another_tatic_method()

....

so you only bring the one name into the current namespace -- for functions that all have somethign to do with the class, it's a cleaner API.

the only functional difference I can think of is that as a staticmethod, it can be overridden in a subclass.

There doesn't appear to be a PEP For this -- 'cause it dates back to Python 2.2 - too bad, it would be nice to see the justification. But it seemed to go along with "new style" classes.

-CHB

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Cody Precord
Hi,

On Thu, May 11, 2017 at 5:45 PM, Chris Barker <[hidden email]> wrote:
On Thu, May 11, 2017 at 10:05 AM, Tim Roberts <[hidden email]> wrote:
If a function can be called without the Le and instance, then what value is their having it in the class at all?
This looks like a friend in C++, which just tears the OO walls apart.

It's not like "friend" at all.  It's closer to a C++ static method.  The @classmethod is declared within the class, and has access to the class variables.


In Python all classes are 'friends' since there is no concept access protection (i.e. no public, private, protected, ect..). Python only has access protection by convention (single underscore
 

There are lots of uses for @staticmethod/@classmethod to create better designed classes, APIs, and improve code quality but as you mentioned there is no absolute functional need to use them. Same thing could be said about classes in C++, could write C++ code that is completely procedural based like C ;)

A common use for classmethod would be say you have a class that you have many instances of but they all may need to share some common state and have methods to work with this common state in this case classmethods provide a nice encapsulated way to achieve this.

The uses for staticmethod might be more often be seen as a way express methods that do not have side effect behaviors or as Chris mentioned as away to encapsulate namespaces or groups of functions together.

I remember some other more clever uses for classmethod (w/__metaclass_) that I had used in Editra in the past but been long enough my memory is getting fuzzy on the details. (like creating smart factory methods, ect..)


Cody

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: is Cody Record still on this list? wxPython 2.8 Application Development Cookbook (Packt)

Chris Barker - NOAA Federal
On Thu, May 11, 2017 at 4:25 PM, Cody <[hidden email]> wrote:
I remember some other more clever uses for classmethod (w/__metaclass_) that I had used in Editra in the past but been long enough my memory is getting fuzzy on the details. (like creating smart factory methods, ect..)

even without metaclass magic, a common use for classmethods is as alternate constructors, i.e.:

datetime.now()

(despite not following pep8 conventions, datetime is a class object, so datetime.now() is a classmethod that creates a new datetime instance using the current time)

-CHB


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.