Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55055 > unrolled thread

class implementation

Started bymarkotaht@gmail.com
First post2013-09-30 01:43 -0700
Last post2013-10-09 00:41 +0100
Articles 20 on this page of 35 — 15 participants

Back to article view | Back to comp.lang.python


Contents

  class implementation markotaht@gmail.com - 2013-09-30 01:43 -0700
    Re: class implementation Peter Otten <__peter__@web.de> - 2013-09-30 11:03 +0200
    Re: class implementation markotaht@gmail.com - 2013-09-30 02:10 -0700
      Re: class implementation Peter Otten <__peter__@web.de> - 2013-09-30 11:27 +0200
    Re: class implementation markotaht@gmail.com - 2013-09-30 05:41 -0700
      Re: class implementation Peter Otten <__peter__@web.de> - 2013-09-30 15:02 +0200
      Re: class implementation Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-30 09:21 -0400
      Re: class implementation Piet van Oostrum <piet@vanoostrum.org> - 2013-09-30 13:32 -0400
      Re: class implementation Dave Angel <davea@davea.name> - 2013-09-30 19:34 +0000
        Re: class implementation 88888 Dihedral <dihedral88888@gmail.com> - 2013-10-01 00:01 -0700
      Re: class implementation Ned Batchelder <ned@nedbatchelder.com> - 2013-09-30 17:28 -0400
        Re: class implementation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-30 23:45 +0000
          Re: class implementation Ethan Furman <ethan@stoneleaf.us> - 2013-09-30 17:31 -0700
      Re: class implementation random832@fastmail.us - 2013-09-30 17:49 -0400
      Python variables?  [was Re: class implementation] Ethan Furman <ethan@stoneleaf.us> - 2013-09-30 15:02 -0700
      Re: Python variables? Ben Finney <ben+python@benfinney.id.au> - 2013-10-01 08:37 +1000
      Re: Python variables?  [was Re: class implementation] Ned Batchelder <ned@nedbatchelder.com> - 2013-09-30 19:45 -0400
        Re: Python variables?  [was Re: class implementation] "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-10-01 01:29 +0100
      Re: Python variables? Ned Batchelder <ned@nedbatchelder.com> - 2013-09-30 19:47 -0400
        Re: Python variables? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-01 01:03 +0000
          Re: Python variables? Ned Batchelder <ned@nedbatchelder.com> - 2013-09-30 21:28 -0400
      Re: class implementation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-01 00:19 +0000
    Re: class implementation markotaht@gmail.com - 2013-10-06 06:15 -0700
      Re: class implementation Terry Reedy <tjreedy@udel.edu> - 2013-10-06 15:52 -0400
      Re: class implementation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-07 01:24 +0000
    Re: class implementation markotaht@gmail.com - 2013-10-08 01:20 -0700
      Re: class implementation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-08 09:41 +0100
      Re: class implementation Dave Angel <davea@davea.name> - 2013-10-08 10:31 +0000
      Re: class implementation Cameron Simpson <cs@zip.com.au> - 2013-10-09 10:55 +1100
        Re: class implementation markotaht@gmail.com - 2013-10-10 11:34 -0700
          Re: class implementation Cameron Simpson <cs@zip.com.au> - 2013-10-11 09:07 +1100
          Re: class implementation Piet van Oostrum <piet@vanoostrum.org> - 2013-10-10 19:29 -0400
            Re: class implementation Ben Finney <ben+python@benfinney.id.au> - 2013-10-11 10:51 +1100
    Re: class implementation markotaht@gmail.com - 2013-10-08 07:05 -0700
      Re: class implementation "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-10-09 00:41 +0100

Page 1 of 2  [1] 2  Next page →


#55055 — class implementation

Frommarkotaht@gmail.com
Date2013-09-30 01:43 -0700
Subjectclass implementation
Message-ID<6e338858-b9e6-4745-9959-35d7c0c7724e@googlegroups.com>
Is there a way to give a class extra functions whidout editing the .py file where the class is located?

[toc] | [next] | [standalone]


#55057

FromPeter Otten <__peter__@web.de>
Date2013-09-30 11:03 +0200
Message-ID<mailman.477.1380531725.18130.python-list@python.org>
In reply to#55055
markotaht@gmail.com wrote:

> Is there a way to give a class extra functions whidout editing the .py
> file where the class is located?

A clean way is subclassing:

import vehicles

class FlyingCar(vehicles.Car):
    def lift_off(self): 
        pass

flying_car = FlyingCar()
flying_car.lift_off()

[toc] | [prev] | [next] | [standalone]


#55058

Frommarkotaht@gmail.com
Date2013-09-30 02:10 -0700
Message-ID<2068fe39-3f52-4724-974a-97a4f8074ed4@googlegroups.com>
In reply to#55055
esmaspäev, 30. september 2013 11:43.19 UTC+3 kirjutas mark...@gmail.com:
> Is there a way to give a class extra functions whidout editing the .py file where the class is located?

But does it have all the variables that the main class have?

[toc] | [prev] | [next] | [standalone]


#55059

FromPeter Otten <__peter__@web.de>
Date2013-09-30 11:27 +0200
Message-ID<mailman.478.1380533199.18130.python-list@python.org>
In reply to#55058
markotaht@gmail.com wrote:

> esmaspäev, 30. september 2013 11:43.19 UTC+3 kirjutas mark...@gmail.com:
>> Is there a way to give a class extra functions whidout editing the .py
>> file where the class is located?
> 
> But does it have all the variables that the main class have?

Yes. You can invoke all methods of Car on a FlyingCar instance. If you don't 
define an __init__() method in FlyingCar the initializer of the parent class 
will be invoked which typically sets a few attributes (I think this is what 
you mean by variables; if not: please clarify).

So just try it out. If you don't get your code to work you can post it here 
and ask for help on an actual piece of Python.

[toc] | [prev] | [next] | [standalone]


#55066

Frommarkotaht@gmail.com
Date2013-09-30 05:41 -0700
Message-ID<02724244-c924-4cf0-9656-71ade6e435c2@googlegroups.com>
In reply to#55055
under variables, i mean, the int's and lists and strings and floats that the parent class uses. IF in parent class there is variable called location, then can i use the same variable in my sub class.

[toc] | [prev] | [next] | [standalone]


#55071

FromPeter Otten <__peter__@web.de>
Date2013-09-30 15:02 +0200
Message-ID<mailman.484.1380546073.18130.python-list@python.org>
In reply to#55066
markotaht@gmail.com wrote:

> under variables, i mean, the int's and lists and strings and floats that
> the parent class uses. IF in parent class there is variable called
> location, then can i use the same variable in my sub class.

Please show us some code. Thankyou. 

[toc] | [prev] | [next] | [standalone]


#55073

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-09-30 09:21 -0400
Message-ID<mailman.485.1380547318.18130.python-list@python.org>
In reply to#55066

[Multipart message — attachments visible in raw view] — view raw

On Mon, Sep 30, 2013 at 9:02 AM, Peter Otten <__peter__@web.de> wrote:

> markotaht@gmail.com wrote:
>
> > under variables, i mean, the int's and lists and strings and floats that
> > the parent class uses. IF in parent class there is variable called
> > location, then can i use the same variable in my sub class.
>
> Please show us some code. Thankyou.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You should start by learning about classes -- perhaps here:
http://docs.python.org/2/tutorial/classes.html#classes

Then, try writing some code using classes or extending a class.  Then come
back when something doesn't work as expected

-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#55093

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-09-30 13:32 -0400
Message-ID<m27gdy5i75.fsf@cochabamba.vanoostrum.org>
In reply to#55066
markotaht@gmail.com writes:

> under variables, i mean, the int's and lists and strings and floats that the parent class uses. IF in parent class there is variable called location, then can i use the same variable in my sub class.

Do you mean class variables or instance variables?

-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [next] | [standalone]


#55106

FromDave Angel <davea@davea.name>
Date2013-09-30 19:34 +0000
Message-ID<mailman.502.1380569672.18130.python-list@python.org>
In reply to#55066
On 30/9/2013 08:41, markotaht@gmail.com wrote:

> under variables, i mean, the int's and lists and strings and floats that the parent class uses. IF in parent class there is variable called location, then can i use the same variable in my sub class.

Python doesn't actually have variables, but the things it documents as
variables are local names within a method.  Those are not visible
outside of the method, regardless of whether you're in a class or a
subclass.

But perhaps you mean attributes.  There are both class attributes and
instance attributes, and the behavior is quite different.  Roughly
speaking a class attribute occurs only once per class, and all code can
read its value with either Class.my_attrib   or  instance.my_attrib.  It
can be written with Class.my_attrib.

On the other hand, instance attributes are usable by 
instance.my_attrib, regardless of whether the instance is a base class
or a child class.  Each instance of the class gets a separate copy of
such an attribute.  They are normally defined in the __init__() method.

If you don't happen to know the difference between a class an an
instance of that class, then all the above will look like gibberish, and
you need to do some studying first.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#55164

From88888 Dihedral <dihedral88888@gmail.com>
Date2013-10-01 00:01 -0700
Message-ID<14031e5f-584f-4796-971f-c0b562351b9b@googlegroups.com>
In reply to#55106
On Tuesday, October 1, 2013 3:34:08 AM UTC+8, Dave Angel wrote:
> On 30/9/2013 08:41, markotaht@gmail.com wrote:
> 
> 
> 
> > under variables, i mean, the int's and lists and strings and floats that the parent class uses. IF in parent class there is variable called location, then can i use the same variable in my sub class.
> 
> 
> 
> Python doesn't actually have variables, but the things it documents as
> 
> variables are local names within a method.  Those are not visible
> 
> outside of the method, regardless of whether you're in a class or a
> 
> subclass.
> 
> 
> 
> But perhaps you mean attributes.  There are both class attributes and
> 
> instance attributes, and the behavior is quite different.  Roughly
> 
> speaking a class attribute occurs only once per class, and all code can
> 
> read its value with either Class.my_attrib   or  instance.my_attrib.  It
> 
> can be written with Class.my_attrib.
> 
> 
> 
> On the other hand, instance attributes are usable by 
> 
> instance.my_attrib, regardless of whether the instance is a base class
> 

An instance is an object of some class
that could have its own attributes
i.e. instance priviate properties
during the run time.

> or a child class.  Each instance of the class gets a separate copy of
> 
> such an attribute.  They are normally defined in the __init__() method.
> 
> 
> 
> If you don't happen to know the difference between a class an an
> 
> instance of that class, then all the above will look like gibberish, and
> 
> you need to do some studying first.
> 
> 
> 
> -- 
> 
> DaveA

[toc] | [prev] | [next] | [standalone]


#55118

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-09-30 17:28 -0400
Message-ID<mailman.510.1380576514.18130.python-list@python.org>
In reply to#55066
On 9/30/13 3:34 PM, Dave Angel wrote:
> Python doesn't actually have variables, but the things it documents as
> variables are local names within a method.  Those are not visible
> outside of the method, regardless of whether you're in a class or a
> subclass.

Why does this meme persist!?  Of course Python has variables, they just 
don't work like C variables do.  If you'd like to know the details: 
http://nedbatchelder.com/text/names.html

--Ned.

[toc] | [prev] | [next] | [standalone]


#55142

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-30 23:45 +0000
Message-ID<524a0d2c$0$29974$c3e8da3$5496439d@news.astraweb.com>
In reply to#55118
On Mon, 30 Sep 2013 17:28:30 -0400, Ned Batchelder wrote:

> On 9/30/13 3:34 PM, Dave Angel wrote:
>> Python doesn't actually have variables, but the things it documents as
>> variables are local names within a method.  Those are not visible
>> outside of the method, regardless of whether you're in a class or a
>> subclass.
> 
> Why does this meme persist!?  Of course Python has variables, they just
> don't work like C variables do.  If you'd like to know the details:
> http://nedbatchelder.com/text/names.html


I'm not convinced that "Python variables are different from C variables" 
is a better way to get through to people than "Python doesn't have 
variables, it has name bindings".

Of course, *technically* the first statement is accurate, and the second 
relies on a definition of variable that is not universal. The question is 
which is more effective at getting the differences between the two 
programming models through to the reader. I can't speak for others, but 
in my own experience, I never *quite* understood the semantic differences 
between Python name bindings and Pascal variables until I came across the 
meme "Python has no variables".



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#55155

FromEthan Furman <ethan@stoneleaf.us>
Date2013-09-30 17:31 -0700
Message-ID<mailman.526.1380588785.18130.python-list@python.org>
In reply to#55142
On 09/30/2013 04:45 PM, Steven D'Aprano wrote:
>
> I can't speak for others, but in my own experience, I never *quite*
> understood the semantic differences between Python name bindings and
> Pascal variables until I came across the meme "Python has no variables".

+1

[toc] | [prev] | [next] | [standalone]


#55122

Fromrandom832@fastmail.us
Date2013-09-30 17:49 -0400
Message-ID<mailman.513.1380577759.18130.python-list@python.org>
In reply to#55066
On Mon, Sep 30, 2013, at 17:28, Ned Batchelder wrote:
> On 9/30/13 3:34 PM, Dave Angel wrote:
> > Python doesn't actually have variables, but the things it documents as
> > variables are local names within a method.  Those are not visible
> > outside of the method, regardless of whether you're in a class or a
> > subclass.
> 
> Why does this meme persist!?  Of course Python has variables, they just 
> don't work like C variables do.  If you'd like to know the details: 
> http://nedbatchelder.com/text/names.html

Wait a minute, how exactly are C variables supposed to be different from
this?

[toc] | [prev] | [next] | [standalone]


#55130 — Python variables? [was Re: class implementation]

FromEthan Furman <ethan@stoneleaf.us>
Date2013-09-30 15:02 -0700
SubjectPython variables? [was Re: class implementation]
Message-ID<mailman.516.1380580012.18130.python-list@python.org>
In reply to#55066
On 09/30/2013 02:28 PM, Ned Batchelder wrote:
> On 9/30/13 3:34 PM, Dave Angel wrote:
>> Python doesn't actually have variables, but the things it documents as
>> variables are local names within a method.  Those are not visible
>> outside of the method, regardless of whether you're in a class or a
>> subclass.
>
> Why does this meme persist!?  Of course Python has variables, they just don't work like C variables do.  If you'd like
> to know the details: http://nedbatchelder.com/text/names.html

Because Python's model is different enough that it usually makes thinking about it simpler to stay away from the word 
'variable'; in every other language I have used a variable is a box, but in Python it's a label for a box.

 From your blog:
>
> Names are Python's variables: they refer to values, and
>  those values can change (vary) over the course of your
>  program.

This is partially incorrect.  If the value referred to by the name is immutable, then it cannot change; perhaps you 
meant to say that which object the name points to can vary over time?

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#55135 — Re: Python variables?

FromBen Finney <ben+python@benfinney.id.au>
Date2013-10-01 08:37 +1000
SubjectRe: Python variables?
Message-ID<mailman.519.1380580637.18130.python-list@python.org>
In reply to#55066
Ethan Furman <ethan@stoneleaf.us> writes:

> From [Ned Batchelder]'s blog:

> > Names are Python's variables: they refer to values, and
> >  those values can change (vary) over the course of your
> >  program.
>
> This is partially incorrect.  If the value referred to by the name is
> immutable, then it cannot change; perhaps you meant to say that which
> object the name points to can vary over time?

I agree. Names are not Python's variables.

If anything in Python is a “variable” as generally understood, it is not
a name. It is a *binding*, from a reference (a name, or some other
reference like a list item) to a value.

It is the binding which can change over the course of the program, so
that is the variable.

-- 
 \        “Good judgement comes from experience. Experience comes from |
  `\                              bad judgement.” —Frederick P. Brooks |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#55141 — Re: Python variables? [was Re: class implementation]

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-09-30 19:45 -0400
SubjectRe: Python variables? [was Re: class implementation]
Message-ID<mailman.523.1380584711.18130.python-list@python.org>
In reply to#55066
On 9/30/13 6:02 PM, Ethan Furman wrote:
> On 09/30/2013 02:28 PM, Ned Batchelder wrote:
>> On 9/30/13 3:34 PM, Dave Angel wrote:
>>> Python doesn't actually have variables, but the things it documents as
>>> variables are local names within a method.  Those are not visible
>>> outside of the method, regardless of whether you're in a class or a
>>> subclass.
>>
>> Why does this meme persist!?  Of course Python has variables, they 
>> just don't work like C variables do.  If you'd like
>> to know the details: http://nedbatchelder.com/text/names.html
>
> Because Python's model is different enough that it usually makes 
> thinking about it simpler to stay away from the word 'variable'; in 
> every other language I have used a variable is a box, but in Python 
> it's a label for a box.
>

It might help C programmers to stay away from "variable," but some 
people claim we should avoid the word so as not to confuse beginners. 
That's just silly.  Beginners have no holdover concepts from C.  Lots of 
languages use the same names and values model that Python does:  
Javascript, Java, Ruby, Lisp, etc.

> From your blog:
>>
>> Names are Python's variables: they refer to values, and
>>  those values can change (vary) over the course of your
>>  program.
>
> This is partially incorrect.  If the value referred to by the name is 
> immutable, then it cannot change; perhaps you meant to say that which 
> object the name points to can vary over time?

Yes, I meant that 1) names refer to values, and 2) a name can refer to 
different values over the course of a program.  Hence, the value varies, 
hence, a variable.

In fact, it's more accurate to say that Python has no constants! :)

--Ned.
>
> -- 
> ~Ethan~

[toc] | [prev] | [next] | [standalone]


#55149 — Re: Python variables? [was Re: class implementation]

From"Rhodri James" <rhodri@wildebst.demon.co.uk>
Date2013-10-01 01:29 +0100
SubjectRe: Python variables? [was Re: class implementation]
Message-ID<op.w382a2pwa8ncjz@gnudebeest>
In reply to#55141
On Tue, 01 Oct 2013 00:45:06 +0100, Ned Batchelder <ned@nedbatchelder.com>  
wrote:

>
> On 9/30/13 6:02 PM, Ethan Furman wrote:
>> From your blog:
>>>
>>> Names are Python's variables: they refer to values, and
>>>  those values can change (vary) over the course of your
>>>  program.
>>
>> This is partially incorrect.  If the value referred to by the name is  
>> immutable, then it cannot change; perhaps you meant to say that which  
>> object the name points to can vary over time?
>
> Yes, I meant that 1) names refer to values, and 2) a name can refer to  
> different values over the course of a program.  Hence, the value varies,  
> hence, a variable.

Yes, except no.  The problem is that word "value", which I can practically  
see morphing its meaning through that paragraph.  Names refer to objects,  
which have values or interpretations or however you choose to say it.   
Some (mutable) objects can change their value, some (immutable) can't.   
Independently, names can refer to different objects, which may or may not  
have different values (or indeed concepts of value).

When you say "The value varies", it begs the question "Which 'the value'?"

> In fact, it's more accurate to say that Python has no constants! :)

Or, alternatively, that Python has many constants, such as all those  
immutable integers cached around the place :-)  What it doesn't have is  
fixed bindings.

-- 
Rhodri James *-* Wildebeest Herder to the Masses

[toc] | [prev] | [next] | [standalone]


#55143 — Re: Python variables?

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-09-30 19:47 -0400
SubjectRe: Python variables?
Message-ID<mailman.524.1380584880.18130.python-list@python.org>
In reply to#55066
On 9/30/13 6:37 PM, Ben Finney wrote:
> Ethan Furman <ethan@stoneleaf.us> writes:
>
>>  From [Ned Batchelder]'s blog:
>>> Names are Python's variables: they refer to values, and
>>>   those values can change (vary) over the course of your
>>>   program.
>> This is partially incorrect.  If the value referred to by the name is
>> immutable, then it cannot change; perhaps you meant to say that which
>> object the name points to can vary over time?
> I agree. Names are not Python's variables.
>
> If anything in Python is a “variable” as generally understood, it is not
> a name. It is a *binding*, from a reference (a name, or some other
> reference like a list item) to a value.
>
> It is the binding which can change over the course of the program, so
> that is the variable.
>

True, but no one calls the binding the variable.  Here is a program:

     x = 4

Every one of us is perfectly comfortable talking about the variable x.  
Don't get hung up on implementation pedantry.  The name x here refers to 
4.  Later it could refer to "four".  The value associated with the name 
x changed.  x is a variable.

--Ned.

[toc] | [prev] | [next] | [standalone]


#55152 — Re: Python variables?

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-10-01 01:03 +0000
SubjectRe: Python variables?
Message-ID<524a1f5c$0$29974$c3e8da3$5496439d@news.astraweb.com>
In reply to#55143
On Mon, 30 Sep 2013 19:47:49 -0400, Ned Batchelder wrote:

> On 9/30/13 6:37 PM, Ben Finney wrote:
>> Ethan Furman <ethan@stoneleaf.us> writes:
>>
>>>  From [Ned Batchelder]'s blog:
>>>> Names are Python's variables: they refer to values, and
>>>>   those values can change (vary) over the course of your program.
>>> This is partially incorrect.  If the value referred to by the name is
>>> immutable, then it cannot change; perhaps you meant to say that which
>>> object the name points to can vary over time?
>> I agree. Names are not Python's variables.
>>
>> If anything in Python is a “variable” as generally understood, it is
>> not a name. It is a *binding*, from a reference (a name, or some other
>> reference like a list item) to a value.
>>
>> It is the binding which can change over the course of the program, so
>> that is the variable.
>>
>>
> True, but no one calls the binding the variable.  Here is a program:
> 
>      x = 4
> 
> Every one of us is perfectly comfortable talking about the variable x.
> Don't get hung up on implementation pedantry.  The name x here refers to
> 4.  Later it could refer to "four".  The value associated with the name
> x changed.  x is a variable.

Your statement is ambiguous -- do you mean the *association* between 
value (object) and name changed, or the value which is associated with 
the name changed?

In the first case, "the value associated with the name x changed" is no 
different from saying "the name binding changed", only longer and more 
ambiguous.

In the second case, if you mean that the association remains the same, 
but the value itself changed, that's demonstrably untrue since 4 is 
immutable. But you know that :-)

I straddle the fence on this dispute... I'll often refer to Python 
variables when, in my option, it doesn't confuse the issue or introduce 
ambiguity, but I feel guilty doing so :-) And I always look for the 
opportunity to introduce the concept of name binding into the discussion. 
I'm just not religious about it.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web