Results 1 to 7 of 7

Thread: [Python] Class methods vs functions

  1. #1
    Join Date
    Jun 2007
    Location
    The Fiery Desert
    Beans
    136
    Distro
    Ubuntu 7.04 Feisty Fawn

    Cool [Python] Class methods vs functions

    I would like to ask for some general guidance on this question.

    I'm working on a project to build an optical ray-tracing program. Not the same as a graphics package, such as POV-Ray, but something that models ray propagation in a lens system. My eventual goal is a GUI-driven program that would be easy to use in an educational environment, or as a fast first-order design tool in non-critical applications.

    The core of the program is sequential surface object. The approach treats a lens system as a sequence of spaces and surfaces. The object belongs to a custom class that I am writing.

    Creating a class takes information describing the sequence of spaces (lengths and indices of refraction) and surfaces (curvatures). The class then calculates matrices representing each element, and one matrix representing the whole system.

    A point of internal contention that I have encountered is what other functions should I include as class methods, and what functions should be defined externally. For example, a function that returns position of each ray at every surface in the system rather than just its final position? Or functions that compute other alternative representations (such as Gaussian reduction or cardinal points and planes)? Or functions that compute propagation in a different way (tNYU trace instead of ABCD matrices)? Partial or reverse propagation?

    Is there any guidance on what should distinguish a method from a function?
    Die writing: I write crazy things
    PhD Wander: I write crazy things about my travels
    RedScout: Lenovo 3000 C100, 1.5GHz Celeron M, 1.25GB RAM, 120GB HDD

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,837
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: [Python] Class methods vs functions

    Quote Originally Posted by Erdaron View Post
    Is there any guidance on what should distinguish a method from a function?
    Define your classes first. Do you have lenses that bend light rays, or light rays that are bent by lenses? Can you define a generic Lens that can be either a SimpleLens or a CompositeLens, and where you can define a CompositeLens as the association of two Lens (Simple or Composite) and a distance... Once you have the right classes, the methods sort of come out naturally...

  3. #3
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: [Python] Class methods vs functions

    The methods should intuitively represent what you want your objects to do. Once you decide how to organize your data model into different classes, it should be pretty easy to figure out what each class should be capable of.

    Functions should perform a more general (not specifically associated with a class) task. For example, "compare phase of two waves" or "sort waves by amplitude". These would be better handled by a static function rather than a class method.

  4. #4
    Join Date
    Jun 2007
    Location
    The Fiery Desert
    Beans
    136
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [Python] Class methods vs functions

    Thank you for the responses!

    Quote Originally Posted by ofnuts View Post
    Define your classes first. Do you have lenses that bend light rays, or light rays that are bent by lenses? Can you define a generic Lens that can be either a SimpleLens or a CompositeLens, and where you can define a CompositeLens as the association of two Lens (Simple or Composite) and a distance... Once you have the right classes, the methods sort of come out naturally...
    In a sequential ray trace, you usually define a system in the following way:
    Code:
    Space 0: thickness, material (index of refraction)
    Surface 0: curvature (inverse of radius)
    Space 1: thickness, material
    Surface 1: curvature
    etc.
    So the simplest example, a single lens in air, would be made up of five elements: three spaces and two surfaces. This is a standard approach in optical engineering. It works for simple systems, and scales easily for arbitrarily complex systems.

    A ray is normally represented as a 2x1 matrix (N rays would be a 2xN matrix), so it doesn't need its own class as I'm using the matrix implementation from numpy.

    @11jmb - what about alternative ways of doing the same thing? Is it alright to use a method to describe the standard way of doing something and then functions for the alternative ways?
    Die writing: I write crazy things
    PhD Wander: I write crazy things about my travels
    RedScout: Lenovo 3000 C100, 1.5GHz Celeron M, 1.25GB RAM, 120GB HDD

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,837
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: [Python] Class methods vs functions

    Quote Originally Posted by Erdaron View Post
    Thank you for the responses!



    In a sequential ray trace, you usually define a system in the following way:
    Code:
    Space 0: thickness, material (index of refraction)
    Surface 0: curvature (inverse of radius)
    Space 1: thickness, material
    Surface 1: curvature
    etc.
    So the simplest example, a single lens in air, would be made up of five elements: three spaces and two surfaces. This is a standard approach in optical engineering. It works for simple systems, and scales easily for arbitrarily complex systems.
    OK, so use an OpticalSystem class.

    Quote Originally Posted by Erdaron View Post
    A ray is normally represented as a 2x1 matrix (N rays would be a 2xN matrix), so it doesn't need its own class as I'm using the matrix implementation from numpy.
    Possibly. But classes can have very simple attributes.

    Quote Originally Posted by Erdaron View Post
    @11jmb - what about alternative ways of doing the same thing? Is it alright to use a method to describe the standard way of doing something and then functions for the alternative ways?
    A class can have several methods, all doing similar things. Or you can have a some generic RayTrace class, that defines a method to bend a LighRay using a set of OpticalSystems, and overload that method to implement your various ways to perform your computation. The advantage is tha the generic stuff is done only once in the generic class (for instance processing a whole set of rays) while the specific is restricted to a couple of methods.

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [Python] Class methods vs functions

    The line is less clearer than it may seem.

    IMO, think about this in the opposite direction. In your head or wherever, design each procedure as a function. Now, if the function always requires an argument of a certain class in order to work, chances are very high that it should be a method of that class.

  7. #7
    Join Date
    Jun 2007
    Location
    The Fiery Desert
    Beans
    136
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [Python] Class methods vs functions

    Quote Originally Posted by nvteighen View Post
    The line is less clearer than it may seem.

    IMO, think about this in the opposite direction. In your head or wherever, design each procedure as a function. Now, if the function always requires an argument of a certain class in order to work, chances are very high that it should be a method of that class.
    Thank you. This is actually a very helpful perspective.
    Die writing: I write crazy things
    PhD Wander: I write crazy things about my travels
    RedScout: Lenovo 3000 C100, 1.5GHz Celeron M, 1.25GB RAM, 120GB HDD

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •