Re: class implementation

2013-10-10 Thread markotaht
kolmapäev, 9. oktoober 2013 2:55.28 UTC+3 kirjutas Cameron Simpson:
 On 08Oct2013 01:20,  wrote:
 
  I cant just subclassing doesent work. It seem the init method of the source 
  class also calls out another class. And the problem is, i can subclass the 
  other class to with the required function but the end result is that it 
  doesent work, since the source class cant accsess the subclass functions. 
 
  
 
  The source code is pykkar. 
 
  
 
  https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py
 
  
 
  I want to add it a new ability called left(). I cant manipulate the source 
  class, cause then my comp will be the only one where the program runs.
 
  
 
  class pykkar_l(Pykkar):
 
  def left(self):
 
  self._world.execute(left)
 
 [...]
 
 
 
 You normally need to call the superclasses' __init__ method as well.
 
 Example:
 
 
 
 def __init__(self):
 
 Pykkar.__init__(self)
 
 ... any of your own init stuff ...
 
 
 
 Likewise for your world_l class.
 
 
 
 BTW, it is conventional to start class names with an upper case letters. Just
 
 style, but it helps other people when reading your code.
 
 
 
 Cheers,
 
 --  
 
 
 It looks like you've got Mister Bus Error installed.- tjc

OK so I did a took time of and read the pykkar code through. abd I found that 
there is a third class i have to implement. 

This Is the pykkar sourcecode
# coding=UTF-8

Intro and usage
===
Pykkar is a virtual robot living in a virtual world. It can step, turn 90 
degrees right,
paint floor tiles, take and put down traffic cones and push boxes. It can detect
whether there is a wall or other obstacle ahead or whether it's standing on a 
painted
tile. It can be commanded by Python code (either procedural or OOP style)

Pykkar's world can be created by a call to ``create_world`` (when using 
procedural style)
or by instantiating class ``World`` (OOP style). Both take single argument, 
which is
a string representation of the world. Lines in the string represent the rows in 
the world
map. Each character represents one tile. Meaning of characters:

#wall
space  plain floor
.painted floor
bbox on plain floor
Bbox on painted floor
^  v   pykkar on plain floor without cone 
 (caret, greater-than, lowercase v, less-than)
N E S W  pykkar on painted floor without cone
1 2 3 4 5 6 7 8 9cone stack on plain floor
Csingle cone on painted floor

Sample:

create_world('''

# #
# 3#

''')

this creates a world where 2x2 floor are is padded with walls. Pykkar is in 
north-west
corner of the floor, looking east and in south-east corner there is a stack of 
3 traffic
cones.

In procedural style, Pykkar can be commanded by calling global functions 
defined in this 
module (eg. ``step()``, ``right()``, etc). There are also functions for 
querying the 
world (``is_wall()``, ``is_box()``, etc). New compound commands can be defined 
by defining 
new functions in client module.

In OOP style, Pykkar is represented by a separate object of class ``Pykkar``. 
In the
start of the program, client code is supposed to create new world (eg. ``w = 
World(layout)``)
and a Pykkar living in that world (eg ``p = Pykkar(w)``). Commands are given by 
calling 
the methods of Pykkar object. New commands should be defined by subclassing 
``Pykkar``. 



Technical stuff

In order to reserve the main thread for executing commands (this way respective 
function calls
can be written in client module's toplevel), tkinter window must run in a 
different thread. 
Unfortunately, tkinter runs reliably only in the main thread of the process. 
For this reason the execution is divided into 2 processes: the main process, 
which is just a shallow command proxy and the child process, which runs actual 
program 
logic and presents the world state in a tkinter window.

Main process (ie. user module) normally begins by creating the world (with 
either 
``create_world(...)`` or ``World(...)``). This spawns a child process which 
creates
tkinter window representing the world. Main process then continues by executing
user-provided function/method calls, which amounts to writing respective 
command strings
to child process' stdin and reading results back from child process' stdout.

Main player in child process is an object of class ``_WorldProper``. It keeps 
the 
data structures about world layout, responds to commands that alter the world 
state and runs 
a tkinter window. It reads periodically next command from stdin, acts upon it 
and writes 
result (may be None) to stdout.

NB! as stdout from tkinter process is parsed, you can't just print out debug 
information
to sys.stdout. Use sys.stderr instead!

Reading from stdin blocks, as usual. This would make window temporariliy 
unresponsive 
when 

Re: class implementation

2013-10-10 Thread Cameron Simpson
On 10Oct2013 11:34, markot...@gmail.com markot...@gmail.com wrote:
 OK so I did a took time of and read the pykkar code through. abd
 I found that there is a third class i have to implement.
 This Is the pykkar sourcecode
[... lots and lots of docstring and code ...]
[... and finally a little more messgae ...]
 I did not wan to but the source in here because it is just so god
 damn long. But some of you wanted it, so here it is :D

As a matter of readability, if I really need to include a huge body
of text I append it below the end of my message, and say something
like this (pretending I were writing your message):

  OK so I did a took time of and read the pykkar code through. abd
  I found that there is a third class i have to implement.

  I've appended the relevant pykkar source below this message.

  So I have come up with this code: [...]

That way your message does not get hidden by the (overly long IMO)
included material and readers can get on with looking at your stuff,
knowing that if necessary they can wade through the other stuff.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

Any profit should go to Arnie's `get the daemon carved on Mount Rushmore' fund.
- Marty Albini, DOD0550, mar...@sdd.hp.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-10 Thread Piet van Oostrum
markot...@gmail.com writes:


 OK so I did a took time of and read the pykkar code through. abd I found that 
 there is a third class i have to implement. 

[...]

 So I have come up with this code
 from pykkar import *

 create_world(
 
 #  #
 # v#
 #  #
 #  #
 #  #
 
 )

 class world_täiend(World):
 def left(self):
 world.excecute(left)
 
 class pykkar_täiend(Pykkar):
 def left(self):
 self._world.excecute(left)

 class _WorldProper_täiend(_WorldProper):
 def _cmd_right(self):

Should that not be _cmd_left?

 headings = (N,E,S,W)
 cur_tile = self._get_current_tile() 
 
 cur_heading_index = headings.index(cur_tile.pykkar_heading)
 new_heading_index = (cur_heading_index - 1) % 4
 cur_tile.pykkar_heading = headings[new_heading_index]
 
 self._update_pykkar_image(cur_tile)


 left()

 When I run my code I get this error.
 Traceback (most recent call last):
   File C:\Users\MarkoPC\Desktop\python\pykkar_test.py, line 21, in module
 class _WorldProper_täiend(_WorldProper):
 NameError: name '_WorldProper' is not defined

from import * doesn't import names that start with underscore (_). So therefore 
_WorldProper is not defined.

from import * is considered bad practice anyway. It is better just to import 
the things you need.

from pykkar import World, Pykkar, _WorldProper

I have looked a bit in this pykkar.py and I think it is badly structured for 
extension. The three classes are too strongly connected and it is difficult to 
get three subclasses connected in the proper way without duplicating code. But 
anyway you will have to do that when you create your world.

-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-10 Thread Ben Finney
Piet van Oostrum p...@vanoostrum.org writes:

 from import * is considered bad practice anyway. It is better just to import 
 the things you need.

 from pykkar import World, Pykkar, _WorldProper

Or, even better, be explicit:

import pykkar

…

foo = pykkar.World()

-- 
 \  “Computer perspective on Moore's Law: Human effort becomes |
  `\   twice as expensive roughly every two years.” —anonymous |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread markotaht
I cant just subclassing doesent work. It seem the init method of the source 
class also calls out another class. And the problem is, i can subclass the 
other class to with the required function but the end result is that it doesent 
work, since the source class cant accsess the subclass functions. 

The source code is pykkar. 

https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py

I want to add it a new ability called left(). I cant manipulate the source 
class, cause then my comp will be the only one where the program runs.

class pykkar_l(Pykkar):
def left(self):
self._world.execute(left)

def _cmd_left(self):
headings = (N,E,S,W)
cur_tile = self._get_current_tile() 

cur_heading_index = headings.index(cur_tile.pykkar_heading)
new_heading_index = (cur_heading_index - 1) % 4
cur_tile.pykkar_heading = headings[new_heading_index]

self._update_pykkar_image(cur_tile)

class world_l(World):
def left(self):
self._world.execute(left)

These are my subclasses. For it to work. Class World, must obtain the method 
from subclass world_l
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread Mark Lawrence

On 08/10/2013 09:20, markot...@gmail.com wrote:

To whom and to what are you replying?

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread Dave Angel
On 8/10/2013 04:20, markot...@gmail.com wrote:

 I cant just subclassing doesent work.

I can't parse that sentence.

 It seem the init method of the source class also calls out another
class. And the problem is, i can subclass the other class to with the required 
function but the end result is that it doesent work, since the source class 
cant accsess the subclass functions. 

What's a source class?  If you mean parent class, then say so.
Otherwise, if you give it a name, we might be able to follow.   But
source and other don't narrow the field very much.

A parent class can certainly access the child class (subclass) 
methods (not functions).  But only if the instance (self) is an instance
of the child class. That's the whole point of subclassing.


 The source code is pykkar. 

 https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py

 I want to add it a new ability called left(). I cant manipulate the source 
 class, cause then my comp will be the only one where the program runs.

 class pykkar_l(Pykkar):
 def left(self):
 self._world.execute(left)

 def _cmd_left(self):
 headings = (N,E,S,W)
 cur_tile = self._get_current_tile() 
 
 cur_heading_index = headings.index(cur_tile.pykkar_heading)
 new_heading_index = (cur_heading_index - 1) % 4
 cur_tile.pykkar_heading = headings[new_heading_index]
 
 self._update_pykkar_image(cur_tile)

 class world_l(World):
 def left(self):
 self._world.execute(left)

 These are my subclasses. For it to work. Class World, must obtain the method 
 from subclass world_l

Then it sounds like you should make sure that the global value world
in that module is an instance of your world_l class, rather than an
instance or World.  And that the proxy is an instance of pykkar_l rather
than of Pykkar.

import pykkar

layout = fdlkjdsljdslfkjsdljfdsf
pykkar.world = world_I(layout)
??? = pykkar_l(pykkar.world)

You don't show your own top-level code, so I can't integrate it in.

By the way, it's conventional to use uppercase for class names, and
lowercase for instances of those classes.

I'm astounded that your class is using eval and multiprocessing before
understanding classes and subclasses.


-- 
DaveA


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread markotaht
Parent class is at the link. 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread Rhodri James

On Tue, 08 Oct 2013 15:05:26 +0100, markot...@gmail.com wrote:


Parent class is at the link.


Please quote some context when you reply.  What link?

Then again, I'm not about to click on some random link someone posts to a  
newsgroup.  Apart from being one of the classic ways to get a virus onto  
my computer, it's rather selfish of you.  While your newsgroup posting  
will stay around essentially forever, the link will eventually rot.  At  
some point in the future, no one will be able to follow that link, so no  
one will be able to learn from what you have done.


Please show your working, it makes it so much easier for the rest of us to  
understand what you meant when you use terms so loosely!


--
Rhodri James *-* Wildebeest Herder to the Masses
--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-08 Thread Cameron Simpson
On 08Oct2013 01:20, markot...@gmail.com markot...@gmail.com wrote:
 I cant just subclassing doesent work. It seem the init method of the source 
 class also calls out another class. And the problem is, i can subclass the 
 other class to with the required function but the end result is that it 
 doesent work, since the source class cant accsess the subclass functions. 
 
 The source code is pykkar. 
 
 https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py
 
 I want to add it a new ability called left(). I cant manipulate the source 
 class, cause then my comp will be the only one where the program runs.
 
 class pykkar_l(Pykkar):
 def left(self):
 self._world.execute(left)
[...]

You normally need to call the superclasses' __init__ method as well.
Example:

def __init__(self):
Pykkar.__init__(self)
... any of your own init stuff ...

Likewise for your world_l class.

BTW, it is conventional to start class names with an upper case letters. Just
style, but it helps other people when reading your code.

Cheers,
-- 
Cameron Simpson c...@zip.com.au

It looks like you've got Mister Bus Error installed.- tjc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-06 Thread markotaht
There is this class file, it has its functions and variables. Now im greating 
my program, that uses the funcions from the class. BUt there are some functions 
missing from the class. So i want to add some extra funtions to the class, 
whidout altering the original source code, but by extending it in my code. But 
for that i need to use some variables that exsist in the original class. Is 
there a way i can acccsess them?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-06 Thread Terry Reedy

On 10/6/2013 9:15 AM, markot...@gmail.com wrote:

There is this class file, it has its functions and variables. Now im greating 
my program, that uses the funcions from the class. BUt there are some functions 
missing from the class. So i want to add some extra funtions to the class, 
whidout altering the original source code, but by extending it in my code. But 
for that i need to use some variables that exsist in the original class. Is 
there a way i can acccsess them?


I though you already got an answer: subclass the class.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-06 Thread Steven D'Aprano
On Sun, 06 Oct 2013 06:15:51 -0700, markotaht wrote:

 There is this class file, it has its functions and variables. 

What's a class file?

Do you mean a file containing only a single class? 


 Now im
 greating my program, that uses the funcions from the class.

They are called methods.


 BUt there
 are some functions missing from the class. So i want to add some extra
 funtions to the class, whidout altering the original source code, but by
 extending it in my code. But for that i need to use some variables that
 exsist in the original class. Is there a way i can acccsess them?

When you say variables, we prefer to say attributes.

A string variable is a variable that holds a string.
An int variable is a variable that holds an int.
A list variable is a variable that holds a list.
A float variable is a variable that holds a float.

So... 

...a class variable is a variable that holds a class, and an instance 
variable is a variable that holds an instance.


When extending classes, you access attributes exactly the same way you 
would access them if you were writing the class in the first place.

class Test(object):
def __init__(self, arg):
self.arg = arg  # Store the arg as an instance attribute.
def method(self):
print(arg is, self.arg)


class NewTest(Test):  # subclass to add new methods
def add_one(self):
return self.arg+1


For experts only: sometimes you need to extend the class itself. You can 
do that by adding methods to the class:

def minus_one(self):
return self.arg-1

NewTest.minus_one = minus_one



-- 
Steven
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-10-01 Thread 88888 Dihedral
On Tuesday, October 1, 2013 3:34:08 AM UTC+8, Dave Angel wrote:
 On 30/9/2013 08:41, markot...@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

-- 
https://mail.python.org/mailman/listinfo/python-list


class implementation

2013-09-30 Thread markotaht
Is there a way to give a class extra functions whidout editing the .py file 
where the class is located?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Peter Otten
markot...@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()


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread markotaht
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?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Peter Otten
markot...@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.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread markotaht
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.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Peter Otten
markot...@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


Re: class implementation

2013-09-30 Thread Joel Goldstick
On Mon, Sep 30, 2013 at 9:02 AM, Peter Otten __pete...@web.de wrote:

 markot...@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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Piet van Oostrum
markot...@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 p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Dave Angel
On 30/9/2013 08:41, markot...@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


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Ned Batchelder

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.
--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread random832
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?
-- 
https://mail.python.org/mailman/listinfo/python-list


Python variables? [was Re: class implementation]

2013-09-30 Thread Ethan Furman

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~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python variables? [was Re: class implementation]

2013-09-30 Thread Ned Batchelder


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~


--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Steven D'Aprano
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Steven D'Aprano
On Mon, 30 Sep 2013 05:41:16 -0700, markotaht 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.

Firstly, in Python circles we prefer to call them attributes rather 
than variables.

Since this is Python, it is trivially easy to test this for yourself. 
Start an interactive Python interpreter, and then in under a dozen lines 
you can test it:

py class Test:
... attr = Hello World!  # Shared class attribute.
...
py class MyTest(Test):
... pass
...
py x = MyTest()
py x.attr
'Hello World!'


Works perfectly! (It would be a funny programming language where it 
didn't, since this is one of the most fundamental parts of inheritance. A 
language that didn't do something equivalent to this couldn't really 
claim to be object-oriented.)



-- 
Steven
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python variables? [was Re: class implementation]

2013-09-30 Thread Rhodri James
On Tue, 01 Oct 2013 00:45:06 +0100, Ned Batchelder n...@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
--
https://mail.python.org/mailman/listinfo/python-list


Re: class implementation

2013-09-30 Thread Ethan Furman

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
--
https://mail.python.org/mailman/listinfo/python-list


Basic class implementation question

2008-04-01 Thread hexusnexus
I can't get call a class for some reason.  This must be one of those
newbie questions I hear so much about:

class wontwork:
def really():
print Hello World

wontwork.really()

This returns (as an error):

Traceback (most recent call last):
  File pyshell#4, line 1, in module
wontwork.really()
TypeError: unbound method really() must be called with wontwork
instance as first argument (got nothing instead)

Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic class implementation question

2008-04-01 Thread Henry Chang
Try this.

class wontwork:
   def really(self):
   print Hello World

wontwork().really()

On Tue, Apr 1, 2008 at 9:43 PM,  [EMAIL PROTECTED] wrote:
 I can't get call a class for some reason.  This must be one of those
  newbie questions I hear so much about:

  class wontwork:
 def really():
 print Hello World

  wontwork.really()

  This returns (as an error):

  Traceback (most recent call last):
   File pyshell#4, line 1, in module
 wontwork.really()
  TypeError: unbound method really() must be called with wontwork
  instance as first argument (got nothing instead)

  Any ideas?
  --
  http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic class implementation question

2008-04-01 Thread Henry Chang
You might want to consult this.

http://www.ibiblio.org/g2swap/byteofpython/read/object-methods.html

On Tue, Apr 1, 2008 at 9:43 PM,  [EMAIL PROTECTED] wrote:
 I can't get call a class for some reason.  This must be one of those
  newbie questions I hear so much about:

  class wontwork:
 def really():
 print Hello World

  wontwork.really()

  This returns (as an error):

  Traceback (most recent call last):
   File pyshell#4, line 1, in module
 wontwork.really()
  TypeError: unbound method really() must be called with wontwork
  instance as first argument (got nothing instead)

  Any ideas?
  --
  http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reuse base-class implementation of classmethod?

2005-11-01 Thread Giovanni Bajo
David Wahler wrote:

 what's the magic needed to reuse the base-class implementation of a
 classmethod?

 class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

 class B(A):
 @classmethod
 def foo(cls, a, b):
  A.foo(cls, a, b)   # WRONG!

 I need to call the base-class classmethod to reuse its
 implementation, but I'd like to pass the derived class as first
 argument. --
 Giovanni Bajo

 See the super object. In your case, it can be used like this:

 class B(A):
 @classmethod
 def foo(cls, a, b):
 super(B, cls).foo(a, b)


Ah thanks, I did not realize that super() was the only way of doing the right
thing here!
-- 
Giovanni Bajo


-- 
http://mail.python.org/mailman/listinfo/python-list


Reuse base-class implementation of classmethod?

2005-10-31 Thread Giovanni Bajo
Hello,

what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
   @classmethod
   def foo(cls, a,b):
   # do something
   pass

class B(A):
@classmethod
def foo(cls, a, b):
 A.foo(cls, a, b)   # WRONG!

I need to call the base-class classmethod to reuse its implementation, but I'd
like to pass the derived class as first argument.
-- 
Giovanni Bajo


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reuse base-class implementation of classmethod?

2005-10-31 Thread David Wahler

Giovanni Bajo wrote:
 Hello,

 what's the magic needed to reuse the base-class implementation of a
 classmethod?

 class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

 class B(A):
 @classmethod
 def foo(cls, a, b):
  A.foo(cls, a, b)   # WRONG!

 I need to call the base-class classmethod to reuse its implementation, but I'd
 like to pass the derived class as first argument.
 --
 Giovanni Bajo

See the super object. In your case, it can be used like this:

class B(A):
@classmethod
def foo(cls, a, b):
super(B, cls).foo(a, b)

This all assumes you want to modify the behavior of foo in a subclass.
If not, of course, you don't need to override it at all.

-- David

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reuse base-class implementation of classmethod?

2005-10-31 Thread Bengt Richter
On Tue, 01 Nov 2005 00:24:37 GMT, Giovanni Bajo [EMAIL PROTECTED] wrote:

Hello,

what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
   @classmethod
   def foo(cls, a,b):
   # do something
   pass

class B(A):
@classmethod
def foo(cls, a, b):
 A.foo(cls, a, b)   # WRONG!

I need to call the base-class classmethod to reuse its implementation, but I'd
like to pass the derived class as first argument.
-- 
Giovanni Bajo


Maybe define a funny-class-method decorator?
(nothing below tested beyond what you see ;-)

  def funnycm(m):
 ... return property(lambda cls: m.__get__(type(cls), type(type(cls
 ...
  class A(object):
 ... @funnycm
 ... def foo(cls, a, b):
 ... print cls, a, b # do something
 ...
  class B(A):
 ... pass  # just inherit
 ...
  a=A()
  a.foo(1,2)
 class '__main__.A' 1 2
  b=B()
  b.foo(1,2)
 class '__main__.B' 1 2

Or more directly, a custom descriptor (with dynamic method replacement for good 
measure ;-)

  class funnycm(object):
 ... def __init__(self, f): self._f = f
 ... def __get__(self, inst, cls=None):
 ... return self._f.__get__(inst is None and cls or type(inst))
 ... def __set__(self, inst, m): self._f = m  # replace method
 ...
  class A(object):
 ... @funnycm
 ... def foo(cls, a, b):
 ... print cls, a, b # do something
 ...
  class B(A):
 ... pass  # just inherit
 ...
  a=A()
  a.foo(1,2)
 class '__main__.A' 1 2
  b=B()
  b.foo(1,2)
 class '__main__.B' 1 2
  A.foo(3,4)
 class '__main__.A' 3 4
  B.foo(3,4)
 class '__main__.B' 3 4
  a.foo = lambda cls, *args: repr(args)
  a.foo('what','now','then?')
 ('what', 'now', 'then?')
  b.foo('eh?')
 ('eh?',)
  B.foo()
 '()'

Vary to taste ;-)

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list