OOP Question

I find this to be a very simple VPython line of code:

ball = sphere(pos=(0, 4, 0), radius=1, color=color.red)

And then one is able to write this:

**ball.radius **(which holds the value 1, because we set the parameter’s value ‘radius’ to value 1)

Normally in Python when you define a function and then use it, you can’t just access a parameter’s value via function_reference.parameter. How is it possible that in VPython one is able to do that?

Example:

def sphere(radius):

… return radius # ???

ball = sphere(radius=1)

ball.radius

This throws an exception at me:

AttributeError: ‘int’ object has no attribute ‘radius’

How can I create my function ‘sphere’ so that I could do ball.radius without the exception (just like I can do in VPython)?

Hi,

···

On 29 May 2013 22:25, Boštjan Mejak mejak.bost@gmail.com wrote:

I find this to be a very simple VPython line of code:

ball = sphere(pos=(0, 4, 0), radius=1, color=color.red)

And then one is able to write this:

**ball.radius **(which holds the value 1, because we set the parameter’s value ‘radius’ to value 1)

Normally in Python when you define a function and then use it, you can’t just access a parameter’s value via function_reference.parameter. How is it possible that in VPython one is able to do that?

Example:

def sphere(radius):

… return radius # ???

ball = sphere(radius=1)

ball.radius

This throws an exception at me:

AttributeError: ‘int’ object has no attribute ‘radius’

How can I create my function ‘sphere’ so that I could do ball.radius without the exception (just like I can do in VPython)?

Like this:

class sphere(object):

def init(self, radius=1, pos=(0, 0), color=None):

self.radius = radius

self.pos = pos

self.color = color

etc…

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”
http://www.infinity77.net

-------------------------------------------------------------

def ask_mailing_list_support(email):

if mention_platform_and_version() and include_sample_app():
    send_message(email)
else:

    install_malware()
    erase_hard_drives()

-------------------------------------------------------------

Because “sphere” is NOT a function.� It’s a class, and you’re
calling its constructor.� “ball” is an object of type “sphere”.
By creating a class.
C:\tmp>python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit
(Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more
information.

class sphere(object):
…���� def init(self,radius):
…�������� self.radius = radius

ball = sphere(radius=1)
ball.radius
1

···

Bo�tjan Mejak wrote:

I find this to be a very simple VPython line of code:

** ball
= sphere(pos=(0, 4, 0), radius=1, color=color.red)**

And then one is able to write this:

ball.radius� (which
holds the value 1, because we set the parameter’s value
‘radius’ to value 1)

    Normally in Python when you define a function and then use it,

you can’t just access a parameter’s value viafunction_reference.parameter . How is it possible that in
VPython one is able to do that?

Example:

def sphere(radius):

… � � � �return radius �# ???

ball = sphere(radius=1)

ball.radius

This throws an exception at me:

      AttributeError: 'int' object has no attribute

‘radius’

      How can I create my function 'sphere' so that I

could do ball.radius without the exception (just like I can do
in VPython)?

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Ah, yes! Thanks, Andrea. :slight_smile:

Perfect answer, Tim. Thank you very much! :wink:

answered (correctly, of course), but for the case of accessing
attributed directly, or wanting it to look that way, google for
"python properties" and read away...

-Chris

···

On Wed, May 29, 2013 at 1:33 PM, Boštjan Mejak <mejak.bost@gmail.com> wrote:

Perfect answer, Tim. Thank you very much! :wink:

--

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

Chris.Barker@noaa.gov