class implementation

M

markotaht

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

Peter Otten

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()
 
M

markotaht

esmaspäev, 30. september 2013 11:43.19 UTC+3 kirjutas (e-mail address removed):
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?
 
P

Peter Otten

esmaspäev, 30. september 2013 11:43.19 UTC+3 kirjutas (e-mail address removed):

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.
 
M

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.
 
P

Peter Otten

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.
 
P

Piet van Oostrum

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?
 
D

Dave Angel

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.
 
E

Ethan Furman

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?
 
N

Ned Batchelder

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:

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.
 
S

Steven D'Aprano

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".
 
N

Ned Batchelder

Ethan Furman said:
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.
 
S

Steven D'Aprano

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.)
 
E

Ethan Furman

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
 
S

Steven D'Aprano

Ethan Furman said:
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.
 
N

Ned Batchelder

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?

Yes, my statement was ambiguous. The value of x at time t1 can be
different than the value of x at time t2, and there are two different
ways it can differ. None of that changes the fact that the value
associated with the name varies over the course of a program, giving
rise to "variables."

I prefer to say that Python has variables, and they work by a mechanism
of names referring to values. I don't find that beginners get it by
being told that Python has no variables. It seems to be something that
experts sometimes find helpful, though.

--Ned.
 
8

88888 Dihedral

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.
 
M

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 theclass, 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top