POV-Ray : Newsgroups : povray.off-topic : OO theory? Python. Server Time
21 Nov 2026 04:30:41 EST (-0500)
  OO theory? Python. (Message 1 to 19 of 19)  
From: Shay
Subject: OO theory? Python.
Date: 13 Oct 2012 13:50:01
Message: <web.5079a76ae05b416e343b23870@news.povray.org>
I'm trying to improve myself in this area. I use objects, but there are some
things I think I just haven't wrapped my head around. I don't personally know
any programmers, so my though process has evolved, for the most part, in
isolation.

OK, starting with the basics. I want to add two vectors. The OO way ...

(as I understand it. Please include "as I understand it" as a standard
disclaimer for anything I write here.)

..... is to make a vector object and define an addition method. My vector would
be
an instance of a Vector class. That would look like:

a = Vector(1, 2, 3)
b = Vector(4, 5, 6)

and I would add them together with

c = a.add(b)

Well, not if I want to keep my sanity, because nothing else in Python works that
way. In Python (and I assume every other language), things that look like

instance.method()

return None and change the state of instance. I don't want to return None OR
change the state of instance, but if I want to avoid bleeding from my eyeballs
in the future, I'd better make sure I stick to that convention. (Sure, there's
also a + b possible, but I want to expand this logic to more complex
operations).

So, I make a function and end up with

c = vector_addition([1, 2, 3], [4, 5, 6])

And then I realize that vector_addition is "good" for other things as well:

c = vector_addition(([1], [2], [3]], [[4], [5], [6]])

is an ugly, sloppy way to get the transverse of a 2xwhatever matrix.

And that could be improved. Instead of the ugly [[1], [2], [3]] stuff, I could
define another functions which add numbers like lists

list_addition(a, b) => [a, b]

Now I can use

c = vector_addition(number_addition, a, b)

or

c = vector_addition(list_addition, a, b)

or functools.partial into vector_like_list_addition(a, b)

And this all works out, because many times there will be no 'a' and 'b'. The
arguments will be used once and never assigned to a variable, much less
instanced into a user-defined class.

Actually that's most of the time. When I want the distance between a point and a
plane, I may have a point object, but I won't have a plane object. I'll just
have a bunch of other data that can be used to describe a plane. The only thing
I need to know about the plane is that it happens to be the plane one of my
triangles is sitting on. I don't need a plane, I need to treat a triangle like a
plane.

Or, there may be tens of thousands of 'a's and 'b's, and they'll be in a file
somewhere which doesn't require my importing an object definition.

So, my thinking is: sometimes I want to stack bricks like bricks, but it's often
handy to stack bricks like rocks or marbles or bananas. And if my banana_stack
function can't handle it, then I've probably poorly (not abstractly enough)
described my bricks.

I get(?) that this is what inheritance is for, but I feel like I'm forcing a
round peg through a square hole when I try it. I've made too many assumptions
before I get to implementation. I'm trying to look at these problems in a
different way FROM THE BEGINNING.

Not every problem can be solved as elegantly as the one above, so I try to use
OO, but my classes turn into monsters. My objects have attributes which are
objects themselves, which have their own attributes which ...

I end up with

A.attr.attr = A.attr.attr.method(A.attr.attr.attr.method(arg))

From the above, it's obvious I'm breaking the instance.method()=>None convention
as well. If I don't, I end up with a module full of object-specific functions
and an object that doesn't do much more than a dictionary. I could do a cleaner
job with an actual dictionary or struct-like class, but I'm /trying/ to learn.

Guidance welcome.

 -Shay


Post a reply to this message

From: Le Forgeron
Subject: Re: OO theory? Python.
Date: 13 Oct 2012 14:33:26
Message: <5079b3f6$1@news.povray.org>
Le 13/10/2012 19:49, Shay nous fit lire :
> In Python (and I assume every other language), things that look like
> 
> instance.method()
> 
> return None and change the state of instance.

Well, stop. Stay away of python if you are hoping to learn OO with it:
it's a snake that would swallow you and as such hide the sun.

For instance, your addition in C++ would probably something along:


// Untested, typo might be there too
class Vector {
private:
  double internal_storage[3];
public:
  Vector(); // allow unspecified value
  Vector(double a, double b, double c);
// constructors are implemented elsewhere to protect the innocent

  Vector & operator +(const Vector &v2) const
  {
     return Vector(
this.internal_storage[0]+v2.internal_storage[0],
this.internal_storage[1]+v2.internal_storage[1],
this.internal_storage[2]+v2.internal_storage[2] );
  }
};

(should rather be name Vector3d... )

Usage

Vector c = Vector(1,2,3)+Vector(4,5,6);
(Bad, I should have also a copy constructor to allow:
 Vector c(Vector(1,2,3)+Vector(4,5,6));
)

Or

Vector c;
Vector a(1,2,3);
Vector b(4,5,6);
c = a+b;

Caveat: there is place for more code, and optimisations too.
(especially with move constructor and copy constructor, but first
understanding reference and const is recommended)

About inheritance, do not try to use it everywhere. A thumb is a finger,
but not all fingers are thumbs, and there is something called head which
is neither. Not talking about the tank of the car.

The importance of OO: it's like a cell from your body, there is an outer
interface and there is the inner content. The inner content must not be
visible from outsiders so that a radical change of it does not propagate
to an overhaul rewrite of all others pieces. If you can keep the outer
interface, there should be very low impact.
(but sometimes you need to nevertheless tie two or more objects... as a
pack of close friends, for efficiency purpose; If you tie everything,
you loose the OO game)

Hint: instead of python, have a look at Java or C++
(they differs: Java has only single inheritance in a row, with interface
to glue additional branches, and C++ allows the dreaded diamond shape,
not for the children)

Holy war java vs C++: fight!


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 13 Oct 2012 14:38:06
Message: <5079b50e@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
>   Vector & operator +(const Vector &v2) const
>   {
>      return Vector(
> this.internal_storage[0]+v2.internal_storage[0],
> this.internal_storage[1]+v2.internal_storage[1],
> this.internal_storage[2]+v2.internal_storage[2] );
>   }

You are returning a reference to a temporary. Kaboom.

-- 
                                                          - Warp


Post a reply to this message

From: Le Forgeron
Subject: Re: OO theory? Python.
Date: 13 Oct 2012 14:49:24
Message: <5079b7b4$1@news.povray.org>
Le 13/10/2012 20:38, Warp nous fit lire :
> Le_Forgeron <jgr### [at] freefr> wrote:
>>   Vector & operator +(const Vector &v2) const
>>   {
>>      return Vector(
>> this.internal_storage[0]+v2.internal_storage[0],
>> this.internal_storage[1]+v2.internal_storage[1],
>> this.internal_storage[2]+v2.internal_storage[2] );
>>   }
> 
> You are returning a reference to a temporary. Kaboom.
> 
Yes. I do not know why I did that (nor put a space before the +).


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 13 Oct 2012 16:31:17
Message: <5079cf95@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
> Le 13/10/2012 20:38, Warp nous fit lire :
> > Le_Forgeron <jgr### [at] freefr> wrote:
> >>   Vector & operator +(const Vector &v2) const
> >>   {
> >>      return Vector(
> >> this.internal_storage[0]+v2.internal_storage[0],
> >> this.internal_storage[1]+v2.internal_storage[1],
> >> this.internal_storage[2]+v2.internal_storage[2] );
> >>   }
> > 
> > You are returning a reference to a temporary. Kaboom.
> > 
> Yes. I do not know why I did that (nor put a space before the +).

Or use "this." instead of "this->". (Or using "this" in the first place...)

-- 
                                                          - Warp


Post a reply to this message

From: Aydan
Subject: Re: OO theory? Python.
Date: 15 Oct 2012 11:55:00
Message: <web.507c30f57215dcd3771cd8e0@news.povray.org>
Adding two objects of class vector:
[begin code]
class vector(object):
  def __init__(self,x,y,z):
    self.__x=x
    self.__y=y
    self.__z=z

  @property
  def x(self):
    return self.__x

  @property
  def y(self):
    return self.__y

  @property
  def z(self):
    return self.__z

  def __add__(self,b):
    """this overloads the + operator"""
    return vector(self.x + b.x, self.y + b.y, self.z + b.z)

  def __sub__(self,b):
    """this overloads the - operator"""
    return vector(self.x - b.x, self.y - b.y, self.z - b.z)

a = Vector(1, 2, 3)
b = Vector(4, 5, 6)
c = a + b
[end code]

in python there's no distinction between method and function.
And for python just like any other OO language:
Every function call can return anything. Any class function/method can but need
not modify the instance. It's all up to you.

Regards
Aydan


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 15 Oct 2012 12:06:15
Message: <507c3477@news.povray.org>
Aydan <hes### [at] hendrik-sachsenet> wrote:
> [begin code]
> class vector(object):
>   def __init__(self,x,y,z):
>     self.__x=x
>     self.__y=y
>     self.__z=z

Is that really how you write OO code in Python? Is it just me, or does it
look like Python hasn't actually been specifically designed to be an OO
language?

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 15 Oct 2012 12:08:59
Message: <507c351b@news.povray.org>
Shay <nomail@nomail> wrote:
> I'm trying to improve myself in this area. I use objects, but there are some
> things I think I just haven't wrapped my head around. I don't personally know
> any programmers, so my though process has evolved, for the most part, in
> isolation.

I think that you might get more responses from the numerous programmers
here if you ask something more concrete in a more concise and clear manner.

I'm not saying this as any kind of criticism or attack. It's just that
your post is a bit long and vague, which makes it difficult to discern
what exactly is it that you have a problem with and what is it that you
are looking for.

Btw, where have you got the idea that methods shouldn't return anything?
I have never heard of such a thing.

-- 
                                                          - Warp


Post a reply to this message

From: Aydan
Subject: Re: OO theory? Python.
Date: 15 Oct 2012 15:00:01
Message: <web.507c5c6a7215dcd9363b2100@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Aydan <hes### [at] hendrik-sachsenet> wrote:
> > [begin code]
> > class vector(object):
> >   def __init__(self,x,y,z):
> >     self.__x=x
> >     self.__y=y
> >     self.__z=z
>
> Is that really how you write OO code in Python? Is it just me, or does it
> look like Python hasn't actually been specifically designed to be an OO
> language?
>
> --
>                                                           - Warp
What do you mean?
Python doesn't have private variables per se and is completely dynamic.
Everything in Python is an object. Functions, classes, modules even integers are
objects.
So python is very much designed for OO.
if you define a variable with "__" it won't be acessible from outside it's scope
without difficulty. For example you can't see it with introspection and you
can't access it even if you know it's declaration. I think it gets prepended
with a hash value on instantiation.


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: OO theory? Python.
Date: 15 Oct 2012 15:23:49
Message: <507c62c5$1@news.povray.org>
On 15/10/2012 07:56 PM, Aydan wrote:
> Everything in Python is an object. Functions, classes, modules even integers are
> objects.
> So python is very much designed for OO.

Everything in JavaScript is an object. However, JavaScript does not have 
the concepts of "classes" or "inheritance" or "encapsulation". Whether 
you consider JavaScript to be "object-oriented" or not depends on what 
you consider "object-oriented" to actually mean...


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 16 Oct 2012 08:06:23
Message: <507d4dbf@news.povray.org>
Aydan <hes### [at] hendrik-sachsenet> wrote:
> Warp <war### [at] tagpovrayorg> wrote:
> > Aydan <hes### [at] hendrik-sachsenet> wrote:
> > > [begin code]
> > > class vector(object):
> > >   def __init__(self,x,y,z):
> > >     self.__x=x
> > >     self.__y=y
> > >     self.__z=z
> >
> > Is that really how you write OO code in Python? Is it just me, or does it
> > look like Python hasn't actually been specifically designed to be an OO
> > language?
> >
> What do you mean?

Well, for one, methods taking 'self' as a parameter seems highly unusual
because in the vast majority of OO programming languages all methods always
have an implicit pseudo-object (usually named 'self' or 'this'). Python
methods taking it explicitly just feels like a kludge that has been tacked
at a later time onto a non-OO language.

Using __ as a syntax to denote a special meaning of functions and variables
is also quite unusual, and likewise feels like a kludge. Programming
languages seldom give any special syntactic meaning to variables and
function names containing underscores. (Instead, they usually use actual
keywords to express, for example, member variable visibility.)

If these (and I'm sure other) syntactic decisions have been made since the
very beginning of Python, I must say that it's... unusual, to put it in
nice terms.

> Everything in Python is an object.

It's quite ambiguous to say that "X is an object".

In programming theory "object" is a synonym for "first-class citizen",
which in turn means that the entity can be stored in variables and data
structures, passed to and returned from subroutines, constructed at
run-time and has an identity (eg. a unique low-level memory address that
can be compared.)

For example a variable of type 'int' in C is an "object" by this definition
(in other words, it's a first-class citizen.) However, one would hardly
classify it as an "object" in the object-oriented sense of the word. For
example, it's not something that can be inherited from (which pretty much
makes it non-OO.)

"Object-oriented language" requires a bit more than just having "everything
is an object" (especially since it's unclear what's meant by "object" in
this context.)

-- 
                                                          - Warp


Post a reply to this message

From: Aydan
Subject: Re: OO theory? Python.
Date: 16 Oct 2012 10:20:01
Message: <web.507d6c5b7215dcd3771cd8e0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Aydan <hes### [at] hendrik-sachsenet> wrote:
> > Warp <war### [at] tagpovrayorg> wrote:
> > > Aydan <hes### [at] hendrik-sachsenet> wrote:
> > > > [begin code]
> > > > class vector(object):
> > > >   def __init__(self,x,y,z):
> > > >     self.__x=x
> > > >     self.__y=y
> > > >     self.__z=z
> > >
> > > Is that really how you write OO code in Python? Is it just me, or does it
> > > look like Python hasn't actually been specifically designed to be an OO
> > > language?
> > >
> > What do you mean?
>
> Well, for one, methods taking 'self' as a parameter seems highly unusual
> because in the vast majority of OO programming languages all methods always
> have an implicit pseudo-object (usually named 'self' or 'this'). Python
> methods taking it explicitly just feels like a kludge that has been tacked
> at a later time onto a non-OO language.
>
> Using __ as a syntax to denote a special meaning of functions and variables
> is also quite unusual, and likewise feels like a kludge. Programming
> languages seldom give any special syntactic meaning to variables and
> function names containing underscores. (Instead, they usually use actual
> keywords to express, for example, member variable visibility.)
>
> If these (and I'm sure other) syntactic decisions have been made since the
> very beginning of Python, I must say that it's... unusual, to put it in
> nice terms.
>
> > Everything in Python is an object.
>
> It's quite ambiguous to say that "X is an object".
>
> In programming theory "object" is a synonym for "first-class citizen",
> which in turn means that the entity can be stored in variables and data
> structures, passed to and returned from subroutines, constructed at
> run-time and has an identity (eg. a unique low-level memory address that
> can be compared.)
>
> For example a variable of type 'int' in C is an "object" by this definition
> (in other words, it's a first-class citizen.) However, one would hardly
> classify it as an "object" in the object-oriented sense of the word. For
> example, it's not something that can be inherited from (which pretty much
> makes it non-OO.)
>
> "Object-oriented language" requires a bit more than just having "everything
> is an object" (especially since it's unclear what's meant by "object" in
> this context.)
>
> --
>                                                           - Warp

I don't know why the pythonians did what they did, but keep in mind that python
is not a compiled language but a dynamic script language. Everything happens at
runtime.
print "\n".join(dir(int)) gives you all member functions of int.
Now you tell me if it's an OO object or not ;o)
As to why self is passed for instance functions? No idea, I see it as something
like main{} for C, it's just done like that and it doesn't disturb me.
I suppose it's a distinction between class functions and instance functions.


Post a reply to this message

From: Shay
Subject: Re: OO theory? Python.
Date: 16 Oct 2012 17:30:00
Message: <web.507dd1d17215dcd343b23870@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Shay <nomail@nomail> wrote:
> > I'm trying to improve myself in this area. I use objects, but there are some
> > things I think I just haven't wrapped my head around. I don't personally know
> > any programmers, so my though process has evolved, for the most part, in
> > isolation.
>
> I think that you might get more responses from the numerous programmers
> here if you ask something more concrete in a more concise and clear manner.
>
> I'm not saying this as any kind of criticism or attack. It's just that
> your post is a bit long and vague, which makes it difficult to discern
> what exactly is it that you have a problem with and what is it that you
> are looking for.
>
> Btw, where have you got the idea that methods shouldn't return anything?
> I have never heard of such a thing.
>
> --
>                                                           - Warp


Post a reply to this message

From: Darren New
Subject: Re: OO theory? Python.
Date: 20 Oct 2012 13:32:18
Message: <5082e022@news.povray.org>
On 10/16/2012 5:06, Warp wrote:
> Well, for one, methods taking 'self' as a parameter seems highly unusual

A function that takes "self" as the first argument is a method. Otherwise 
it's a function.

> Using __ as a syntax to denote a special meaning of functions and variables
> is also quite unusual,

The constructor is named __init__

The operator+ method is named __add__

And so on. It just avoids special syntax and substitutes special names.

The variable __y is just a plain old variable with the name mangled for 
pseudoreadability, exactly like naming a member variable in C++ m_xyz so you 
remember it's a member variable when you use it. Personally, I never saw the 
point. But since Python doesn't have "private" vs "public" variables, people 
have taken to using leading underscores to mean "don't touch this from outside."

 > Programming
> languages seldom give any special syntactic meaning to variables and
> function names containing underscores. (Instead, they usually use actual
> keywords to express, for example, member variable visibility.)

Unless it, say, a destructor in C++ ;-)

Honestly, I always thought naming the constructor after the class was a 
terrible decision.

> very beginning of Python, I must say that it's... unusual, to put it in
> nice terms.

It *is* unusual. It's not especially broken.

-- 
Darren New, San Diego CA, USA (PST)
   "They're the 1-800-#-GORILA of the telecom business."


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 21 Oct 2012 02:39:52
Message: <508398b8@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> On 10/16/2012 5:06, Warp wrote:
> > Well, for one, methods taking 'self' as a parameter seems highly unusual

> A function that takes "self" as the first argument is a method. Otherwise 
> it's a function.

Well, duh. How does that change what I said?

> > Using __ as a syntax to denote a special meaning of functions and variables
> > is also quite unusual,

> The constructor is named __init__

> The operator+ method is named __add__

> And so on. It just avoids special syntax and substitutes special names.

It substitutes special syntax by using special syntax... Sounds
contradictory to me.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: OO theory? Python.
Date: 21 Oct 2012 20:28:34
Message: <50849332@news.povray.org>
On 10/20/2012 23:39, Warp wrote:
> Darren New<dne### [at] sanrrcom>  wrote:
>> On 10/16/2012 5:06, Warp wrote:
>>> Well, for one, methods taking 'self' as a parameter seems highly unusual
>
>> A function that takes "self" as the first argument is a method. Otherwise
>> it's a function.
>
> Well, duh. How does that change what I said?

I'm just explaining why people pass "self" as the first parameter. It's 
basically a declaration.

>>> Using __ as a syntax to denote a special meaning of functions and variables
>>> is also quite unusual,
>
>> The constructor is named __init__
>
>> The operator+ method is named __add__
>
>> And so on. It just avoids special syntax and substitutes special names.
>
> It substitutes special syntax by using special syntax... Sounds
> contradictory to me.

Having underlings as part of the name isn't any more "special" than it is in 
C or C++.  However, I cannot name my own function "operator'" in C++ because 
the "operator" keyword actually is special syntax. I can name my function 
__quote__ in Python because that function name has no special meaning to the 
interpreter.

When the interpreter sees "a + b" it first looks to see who implements 
__add__, and then invokes that. If neither side implements __add__ then it 
goes to the default implementation of adding integers or floats or whatever.

However, my main point was to explain to you what was happening. Whether you 
think it's reasonable after it is explained is up to you.

-- 
Darren New, San Diego CA, USA (PST)
   "They're the 1-800-#-GORILA of the telecom business."


Post a reply to this message

From: Darren New
Subject: Re: OO theory? Python.
Date: 21 Oct 2012 20:58:26
Message: <50849a32$1@news.povray.org>
On 10/20/2012 23:39, Warp wrote:
> Darren New<dne### [at] sanrrcom>  wrote:
>> On 10/16/2012 5:06, Warp wrote:
>>> Well, for one, methods taking 'self' as a parameter seems highly unusual
>
>> A function that takes "self" as the first argument is a method. Otherwise
>> it's a function.
>
> Well, duh. How does that change what I said?

Well, to be more specific, you can call a function two ways:

abc(xyz, pdq)

or

xyz.abc(pdq)

They both do the same thing, except the second syntax says "pass xyz as the 
first argument to the function abc() found in the dictionary of the value of 
xyz."  So it's not really changing the syntax or declaring that it's a 
method in any way. It's just saying "I expect you to invoke this in a way 
that looks like a method, because I named the first argument 'self'." If you 
wanted to invoke it on some other object, you could just as easily invoke 
abc(that, pdq) and "self" would become "that".

It's a naming convention, nothing more. Unlike, say, the names of 
constructors and destructors, which things like Java, C++, and C# treat as 
special because they match the name of the enclosing class.

Remember, too, that names in Python are merely variables. A class doesn't 
have a name, but only the variable(s)/dictionary key(s) to which it is 
currently assigned.  So there's no way to identify special properties of a 
class, especially since you can modify the classes at run time.

-- 
Darren New, San Diego CA, USA (PST)
   "They're the 1-800-#-GORILA of the telecom business."


Post a reply to this message

From: Warp
Subject: Re: OO theory? Python.
Date: 22 Oct 2012 09:53:59
Message: <50854ff7@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Having underlings as part of the name isn't any more "special" than it is in 
> C or C++.  However, I cannot name my own function "operator'" in C++ because 
> the "operator" keyword actually is special syntax.

'operator' is a keyword that's fairly descriptive. '__' in a variable name
as required syntax isn't very descriptive.

> I can name my function 
> __quote__ in Python because that function name has no special meaning to the 
> interpreter.

That's contradictory with:

> When the interpreter sees "a + b" it first looks to see who implements 
> __add__, and then invokes that.

So it does have a special meaning to the interpreter, or it doesn't?

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: OO theory? Python.
Date: 22 Oct 2012 10:26:30
Message: <50855796$1@news.povray.org>
On 10/22/2012 6:53, Warp wrote:
> Darren New<dne### [at] sanrrcom>  wrote:
>> Having underlings as part of the name isn't any more "special" than it is in
>> C or C++.  However, I cannot name my own function "operator'" in C++ because
>> the "operator" keyword actually is special syntax.
>
> 'operator' is a keyword that's fairly descriptive. '__' in a variable name
> as required syntax isn't very descriptive.

That's a fair complaint.

>> I can name my function
>> __quote__ in Python because that function name has no special meaning to the
>> interpreter.
>
> That's contradictory with:
>
>> When the interpreter sees "a + b" it first looks to see who implements
>> __add__, and then invokes that.
>
> So it does have a special meaning to the interpreter, or it doesn't?

As I thought I said, __add__ has special meaning, __quote__ does not. It's 
not the presence of the __ that makes it magic. It's the entire name.

-- 
Darren New, San Diego CA, USA (PST)
   "They're the 1-800-#-GORILA of the telecom business."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.