Re: [Tutor] dictionary keys

2014-04-08 Thread Peter Otten
Alex Kleider wrote:

> On 2014-04-08 14:34, Peter Otten wrote:
> 
>> That's a change in Python 3 where dict.keys() no longer creates a list,
>> but
>> instead creates a view on the underlying dict data thus saving time and
>> space. In the rare case where you actually need a list you can
>> explicitly
>> create one with
>> 
>> ips = list(ipDic)

> Thanks, Peter, for this clarification.  I want to present the list
> sorted so probably this is the rare case of which you spoke where I
> would need to use l = list(myDict) rather than the view.

You can create and sort the list in a single step:

l = sorted(myDict)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Danny Yoo
>
>
> if line.strip()
>
> Is that stripping the line of white space at the same time that it is
> testing it?
>
>

Two features about Python:

1.  Strings are immutable, so the above is computing what a
whitespace-stripped line would look like.  So that means that
'line.strip()' is doing just a computation: it's not mutating the original
line, but computing a new string that has its leading and trailing
whitespace stripped away.


2.  Empty strings are treated as false values.  I'm not happy with how
loose Python treats truth, and would rather prefer:

if line.strip() != "": ...

so that the thing being tested is explicitly either True or False.  I like
my truth to be black and white, but I suppose I'll have to grimace and bear
the fuzziness.  :P


Together, we see those two features allow us to look at the test in the
Python code:

   if line.strip(): ...

and rephrase it in English as:

   "If the line consists of at least one non-whitespace character: ..."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary keys

2014-04-08 Thread Alex Kleider

On 2014-04-08 14:34, Peter Otten wrote:

That's a change in Python 3 where dict.keys() no longer creates a list, 
but

instead creates a view on the underlying dict data thus saving time and
space. In the rare case where you actually need a list you can 
explicitly

create one with

ips = list(ipDic)




That's because the above is a session using Python 2. Compare:

$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.

dict(a=1, b=2).keys()

dict_keys(['b', 'a'])

$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

dict(a=1, b=2).keys()

['a', 'b']

PS: You can get a view in Python 2, too, with dict.viewkeys()



Thanks, Peter, for this clarification.  I want to present the list 
sorted so probably this is the rare case of which you spoke where I 
would need to use l = list(myDict) rather than the view.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Steven D'Aprano
On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen wrote:
> Hello,
> Could someone explain why and how this list comprehension with strip()
> works?
> 
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
> 
> I had a very long file of strings filled with blank lines I wanted to
> remove. I did some Googling and found the above code snippet, but no clear
> explanation as to why it works. I'm particularly confused by how "if
> t.strip()" is removing the blank lines. 

It isn't. Rather, what it is doing is *preserving* the non-blank lines.

The call to strip() removes any leading and trailing whitespace, so if 
the line is blank of contains nothing but whitespace, it reduces down to 
the empty string:

py> ''.strip()
''

Like other empty sequences and containers, the empty string is 
considered to be "like False", falsey:

py> bool('')
False


So your list cmprehension (re-written to use a more meaningful name) 
which looks like this:

[line for line in f.readlines() if line.strip()

iterates over each line in the file, tests if there is anything left 
over after stripping the leading/trailing whitespace, and only 
accumulates the lines that are non-blank. It is equivalent to this 
for-loop:

accumulator = []
for line in f.readlines():
if line.strip():  # like "if bool(line.strip())"
accumulator.append(line)


> I also don't fully understand the 'print "".join(t)'.

I presume you understand what print does :-) so it's only the "".join(t) 
that has you confused. This is where the interactive interpreter is 
brilliant, you can try things out for yourself and see what they do. Do 
you know how to start the interactive interpreter?

(If not, ask and we'll tell you.)

py> t = ['Is', 'this', 'the', 'right', 'place', 'for', 'an', 'argument?']
py> ''.join(t)
'Isthistherightplaceforanargument?'
py> ' '.join(t)
'Is this the right place for an argument?'
py> '--+--'.join(t)
'Is--+--this--+--the--+--right--+--place--+--for--+--an--+--argument?'


In your case, you have a series of lines, so each line will end with a 
newline:

py> t = ['line 1\n', 'line 2\n', 'line 3\n']
py> ''.join(t)
'line 1\nline 2\nline 3\n'
py> print ''.join(t)
line 1
line 2
line 3


> The above didn't remove the leading white space on several lines, so I made
> the following addition:
> 
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> s = [x.lstrip() for x in t]
> print "".join(s)


You can combine those two list comps into a single one:

f = open('file.txt')
lines = [line.lstrip() for line in f.readlines() if line.strip()]
f.close()


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Ben Finney
Jared Nielsen  writes:

> I had a very long file of strings filled with blank lines I wanted to
> remove. I did some Googling and found the above code snippet

The code you found is one of several syntactic shortcuts in Python,
which allow creating a sequence directly from an expression in your
code.

For explanations, look at the documentation for “generator expression”
and “display” (the latter have syntax for list comprehension, set
comprehension, and dict comprehension).


https://docs.python.org/3/reference/expressions.html#generator-expressions>

https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries>

> I'm particularly confused by how "if t.strip()" is removing the blank
> lines.

The ‘str.strip’ method returns a new string, constructed from the
original by removing all leading and trailing whitespace
https://docs.python.org/3/library/stdtypes.html#str.strip>.

A string, like any object, can be used in a boolean context; that's why
“if some_expression” works for any expression
https://docs.python.org/3/reference/expressions.html#boolean-operations>.

For an expression that returns a string, the value in a boolean context
will be false if the string is empty, and true for any other string.

So, in the list comprehension you found: the “if t.strip()” is getting a
new string, testing it in a boolean context which will be false when the
string is empty, and that condition is what determines which values will
end up in the sequence.

> I also don't fully understand the 'print "".join(t)'.

Each text string object has a ‘join’ method, which uses that string and
the specified sequence to construct a new string, joining all the items
together https://docs.python.org/3/library/stdtypes.html#str.join>.

> The above didn't remove the leading white space on several lines

Right. The new “stripped” string is not used except in the “if” clause,
to determine which values will end up in the new list. The original values
themselves are unchanged by the process, and end up in the new list as
they began.

> List comprehensions are still magic to me. How would I go about
> incorporating lstrip() in the first list comprehension?

In general, if something is a mystery to you, look up the formal
description of the method or syntax in the documentation and carefully
follow what it's telling you.

Have a careful read of the documentation for those concepts, experiment
based on your reading, and see what questions you have after that.

-- 
 \ “Smoking cures weight problems. Eventually.” —Steven Wright |
  `\   |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Walter Prins
Hi,

On 8 April 2014 22:38, Jared Nielsen  wrote:
> Hello,
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
>
> I had a very long file of strings filled with blank lines I wanted to
> remove. I did some Googling and found the above code snippet, but no clear
> explanation as to why it works. I'm particularly confused by how "if
> t.strip()" is removing the blank lines. I also don't fully understand the
> 'print "".join(t)'.

The list comprehension loops through each item in f.readlines(), and
outputs each item (adds it to the output list being constructed), if
and only if the "if" filter condition is true.  Now, to understand "if
t.strip()" you need to understand that Python allows objects and items
which are not explicitly bool types in contexts where a boolean is
required, such as in if statements.

In the case of strings, a blank/empty string is considered False,
while a non-blank/empty string is considered True.  As a consequence,
if t.strip() is equivalent to writing if t.strip() != '', and so its
presence effectively suppresses adding empty lines from the file into
the output list t.  An empty string is said to be "falsy" while a
non-empty string is said to be "truthy".  For more see Section 5.1
(Truth value testing), here:
https://docs.python.org/2/library/stdtypes.html

As for the question about the print statement, firstly read the
following documentation page that describes the string str.join()
method: https://docs.python.org/2/library/stdtypes.html#str.join

>From this, you should be able to infer that what "".join(t) does is to
effectively construct a new output string by concatenating all the
items in t, using a blank string as the delimiter, e.g. the result is
effectively to just concatenate all the strings directly with no
additional delimiter.

> The above didn't remove the leading white space on several lines, so I made
> the following addition:
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> s = [x.lstrip() for x in t]
> print "".join(s)
>
> List comprehensions are still magic to me. How would I go about
> incorporating lstrip() in the first list comprehension?

The output expression for each item output in the list comprehension,
that's the bit n front of the "for", is something you specify/control.
 Now, in your original code, you just output the line that was read
from the file verbatim (e.g. "t"), but nothing forces this on you --
instead you can write any expression you like, including calling
"lstrip" on t, as in your question, e.g simply.

t = [t.lstrip() for t in f.readlines() if t.strip()]

HTH,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Danny Yoo
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)


Hi Jared,


Let me rewrite this without the list comprehension, while preserving behavior.

##
inputFile = open('file.txt')
lines = []
for line in inputFile.readlines():
if line.strip():
lines.append(line)
inputFile.close()
print "".join(lines)
##

I am changing the names of the variables from the original code
because I find it very difficult to distinguish 't' from 'f'
sometimes, and because those names are very tied in my mind to
something else entirely ("true" and "false").


Does the above code make more sense to you than the version using the
list comprehension syntax, or is there something there that is still
confusing?


Good luck to you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary keys

2014-04-08 Thread Peter Otten
Alex Kleider wrote:

> I've got a fairly large script that uses a dictionary (called 'ipDic')
> each
> value of which is a dictionary which in turn also has values which are
> not
> simple types.
> Instead of producing a simple list,
> """
> ips = ipDic.keys()
> print(ips)
> """
> yields
> """
> dict_keys(['61.147.107.120', '76.191.204.54', '187.44.1.153'])
> """
> 
> Searching my code for 'dict_keys' yields nothing.  I've no idea where it
> comes from.
> 
 
> 
> Can anyone shed light on why instead of getting the 
> I
> get "dict_keys(  )"?
> 
> (Using Python3, on Ubuntu  12.4)

That's a change in Python 3 where dict.keys() no longer creates a list, but 
instead creates a view on the underlying dict data thus saving time and 
space. In the rare case where you actually need a list you can explicitly 
create one with

ips = list(ipDic)

> I've been unable to reproduce this behaviour using simpler dictionaries
> which seem to work as I expect:

 d = dict(a=1, b=2, c=3)
 d
> {'a': 1, 'c': 3, 'b': 2}
 d.keys()
> ['a', 'c', 'b']
 print(d.keys())
> ['a', 'c', 'b']

That's because the above is a session using Python 2. Compare:

$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dict(a=1, b=2).keys()
dict_keys(['b', 'a'])

$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dict(a=1, b=2).keys()
['a', 'b']

PS: You can get a view in Python 2, too, with dict.viewkeys()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] dictionary keys

2014-04-08 Thread Alex Kleider
I've got a fairly large script that uses a dictionary (called 'ipDic') 
each
value of which is a dictionary which in turn also has values which are 
not

simple types.
Instead of producing a simple list,
"""
ips = ipDic.keys()
print(ips)
"""
yields
"""
dict_keys(['61.147.107.120', '76.191.204.54', '187.44.1.153'])
"""

Searching my code for 'dict_keys' yields nothing.  I've no idea where it 
comes from.


I've been unable to reproduce this behaviour using simpler dictionaries 
which seem to work as I expect:

d = dict(a=1, b=2, c=3)
d

{'a': 1, 'c': 3, 'b': 2}

d.keys()

['a', 'c', 'b']

print(d.keys())

['a', 'c', 'b']




Can anyone shed light on why instead of getting the  
I

get "dict_keys(  )"?

(Using Python3, on Ubuntu  12.4)

Thanks,
Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] question about strip() and list comprehension

2014-04-08 Thread Jared Nielsen
Hello,
Could someone explain why and how this list comprehension with strip()
works?

f = open('file.txt')
t = [t for t in f.readlines() if t.strip()]
f.close()
print "".join(t)

I had a very long file of strings filled with blank lines I wanted to
remove. I did some Googling and found the above code snippet, but no clear
explanation as to why it works. I'm particularly confused by how "if
t.strip()" is removing the blank lines. I also don't fully understand the
'print "".join(t)'.

The above didn't remove the leading white space on several lines, so I made
the following addition:

f = open('file.txt')
t = [t for t in f.readlines() if t.strip()]
f.close()
s = [x.lstrip() for x in t]
print "".join(s)

List comprehensions are still magic to me. How would I go about
incorporating lstrip() in the first list comprehension?

Many thanks,
J.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Block highlighters in python

2014-04-08 Thread Steven D'Aprano
On Tue, Apr 08, 2014 at 11:18:40AM +0530, Santosh Kumar wrote:
> Is there a way by which we can highlight a block in the python ?
> 
> i have a huge code and i want to see a block of ``if`` code. How do i
> achieve this in VIM or any other editor.
> 
> Note: In perl if i put my cursor on one "{" it will hightlight the other
> closed "}".
> 
> Do we have any such facility in python ?

This has nothing to do with Python, it is completely to do with the 
editing capabilities of your editor. If you are using Windows Notepad, 
it is very weak and has nearly no functionality. If you are using Emacs, 
it can do nearly anything, perhaps with a little bit of scripting.

I don't use Vim, so I cannot answer your question. But in my case, I 
always use four spaces for indents so I can easily see changes in indent 
level:

 # not this
 if condition:
  do_this()
  while condition:
   do_something()
  do_that()


 # make it obvious
 if condition:
 do_this()
 while condition:
 do_something()
 do_that()



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inheritance in classes

2014-04-08 Thread Steven D'Aprano
On Tue, Apr 08, 2014 at 11:14:36AM +0530, Santosh Kumar wrote:
> Can i mask the parent attibutes in the child. let me give a quick example.
> 
> In [1]: class a:
>...: value1 = 1
>...: value2 = 2
>...:
> 
> In [2]: class b(a):
>...: value3 = 3
>...:

All of value1, value2, value3 here are *class attributes*, bound to the 
class, not the instance. In Java terms, that is similar to static 
variables.

For the purpose of your example, that is not very important, but it can 
make a difference.


> In [3]: obj1 = b()
> 
> In [4]: obj1.value1
> Out[4]: 1
> 
> In [5]: obj1.value2
> Out[5]: 2
> 
> In [6]: obj1.value3
> Out[6]: 3
> 
> If you notice in the below example you will see that the child class object
> ``obj1`` has inherited all the attibutes of the parent class.


That is how object oriented programming is supposed to work. If you 
don't want to inherit the attributes of class "a", you should not 
inherit from class "a".


> Is there a
> way by which i can make the child class not inherit some of the properites
> of parent class.

There is, but *you should not do this*. This is poor design, and 
violates the Liskov Substitution Principle:

https://en.wikipedia.org/wiki/Liskov_substitution_principle
http://www.oodesign.com/liskov-s-substitution-principle.html

But what we can do is inherit from a, but over-ride access to one of the 
attributes and fake an attribute error. But really, you should not do 
this -- it is poor design.

py> class A:
... value1 = 23
... value2 = 42
...
py> class B(A):
... value3 = 17
... @property
... def value1(self):
... raise AttributeError("Fake!")
...
py> obj = B()
py> obj.value1
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 5, in value1
AttributeError: Fake!
py> obj.value2
42
py> obj.value3
17


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Constructs

2014-04-08 Thread Steven D'Aprano
On Tue, Apr 08, 2014 at 11:10:57AM +0530, Santosh Kumar wrote:
>   1 #!/usr/bin/python
>   2
>   3 class shape:
>   4   def __init__(self,x,y):
>   5 self.x = x
>   6 self.y = y
>   7   description = "This shape has not been described yet"
>   8   author = "Nobody has claimed to make this shape yet"

Notice that the body of the __init__ method is indented (by two spaces 
-- four is recommended) from the method header. The body ends once the 
indentation returns to the previous level. So this piece of code has 
three levels of indentation:

Level 0: "class shape" is not indented;

Level 1: "def __init__" is 1 indent in;

Level 2: the body of the method is 2 indents in;

Level 1: the "description" and "author" lines are outdented 
 from 2 back to 1.

Because "description" and "author" are indented level with the __init__ 
definition (NOT the body of the method, the def header) that makes them 
*class attributes*. They are bound to the class itself, not the 
instance, and are shared by all instances. In Java terms they would be 
called "static variables".


>   9
>  10   def __init__(self,x,y,z):
>  11 self.x = x
>  12 self.y = y
>  13 self.z = z

This now overwrites the existing __init__ method with a new method, also 
called __init__, that takes three arguments instead of two. Because 
methods are values exactly the same as strings, floats, bools and so 
forth, you can only have one method with the same name. If you wrote:

x = 1
x = 2

of course you would expect that the second assignment to x overwrites 
the first assignment -- x cannot have two different values at the same 
time. The same applies to methods: you cannot have __init__ set to a 
method taking arguments x, y, z and a method taking arguments x, y at 
the same time. The newest assignment wins.

Java methods are fixed at compile-time, so it is easy for the compiler 
to decide which constructor method to call at compile-time: if there are 
two arguments, call the first method, if there are three, call the 
second. But Python methods are values, and can be replaced on the fly at 
run-time. Python can do run-time polymorphism, but not in the same way 
that you do it with Java. Instead, you should define the method with 
default arguments:

def __init__(self, x, y, z=None):
self.x = x
self.y = y
if z is not None:
self.z = z


>  14   print "The values are %d,%d,%d" %(self.x,self.y,self.z)

This line is outdented relative to the body of the __init__ 
method, so it too is at the class scope. That means that the print 
statement will be executed at class definition time. The problem is, at 
class definition time, there is no instance yet, even the class doesn't 
exist yet, so the reference to "self" will fail with NameError.

I think what you want is for the print to be indented one more level, 
so it is inside the method:

def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
print "The values are %d,%d,%d" % (self.x, self.y, self.z)



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inheritance in classes

2014-04-08 Thread David Palao
2014-04-08 7:44 GMT+02:00 Santosh Kumar :
> Can i mask the parent attibutes in the child. let me give a quick example.
>
> In [1]: class a:
>...: value1 = 1
>...: value2 = 2
>...:
>
> In [2]: class b(a):
>...: value3 = 3
>...:
>
> In [3]: obj1 = b()
>
> In [4]: obj1.value1
> Out[4]: 1
>
> In [5]: obj1.value2
> Out[5]: 2
>
> In [6]: obj1.value3
> Out[6]: 3
>
> If you notice in the below example you will see that the child class object
> ``obj1`` has inherited all the attibutes of the parent class. Is there a way
> by which i can make the child class not inherit some of the properites of
> parent class.
>
>
> --
> D. Santosh Kumar
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

Hi,
This is the default behaviour. You can ovrcome that, but it will
require extra work. For instance you could make use of pseudoprivate
attributes (any attribute starting with double underscore, but not
ending with dtwo underscores); or some managing attributes tool:
*) __getattr__, __getattribute__ are generic ways to manage attribute fetching
*) properties and descriptors allow a more specific way to control
attributes (one by one)
But, be careful. These tools can be very tricky at first.
Hope it helps.

Best
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Block highlighters in python

2014-04-08 Thread Alan Gauld

On 08/04/14 06:48, Santosh Kumar wrote:

Is there a way by which we can highlight a block in the python ?

i have a huge code and i want to see a block of ``if`` code. How do i
achieve this in VIM or any other editor.

Note: In perl if i put my cursor on one "{" it will hightlight the other
closed "}".

Do we have any such facility in python ?


This is not a feature of Python but of the IDE or editor.
I don't know of any editor that does that although several
have folding that can collapse a block (one example is Scite)
so it should be possible.

Some of the Python specific IDEs or the Python plugins for
Netbeans or Eclipse etc might support it.

In vim, if you put blank lines between your blocks the {}
keys will move yo to beginning/end of block (ie paragraph).
That's how I tend to do it... But that doesn't match where
you have nested blocks, inside a function say and you want
the outer block...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inheritance in classes

2014-04-08 Thread Alan Gauld

On 08/04/14 06:44, Santosh Kumar wrote:

Can i mask the parent attibutes in the child. let me give a quick example.

In [1]: class a:
...: value1 = 1
...: value2 = 2
...:

In [2]: class b(a):
...: value3 = 3
...:



Note that these are class variables and not instance
variables.



In [3]: obj1 = b()

In [4]: obj1.value1
Out[4]: 1

In [6]: obj1.value3
Out[6]: 3

If you notice in the below example you will see that the child class
object ``obj1`` has inherited all the attibutes of the parent class.


Yes that's what inheritance means.


there a way by which i can make the child class not inherit some of the
properites of parent class.


No.
But you can change the inherited values by masking them with your local 
versions, which could be None.


class c(a):
   value1 = None

obj2 = c()
print(obj2.value1)  -> None


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Constructs

2014-04-08 Thread Alan Gauld

On 08/04/14 06:40, Santosh Kumar wrote:

   1 #!/usr/bin/python
   2
   3 class shape:
   4   def __init__(self,x,y):
   5 self.x = x
   6 self.y = y
   7   description = "This shape has not been described yet"
   8   author = "Nobody has claimed to make this shape yet"
   9
  10   def __init__(self,x,y,z):
  11 self.x = x
  12 self.y = y
  13 self.z = z
  14   print "The values are %d,%d,%d" %(self.x,self.y,self.z)
  15
  16 triangle = shape(100,20,30)
  17 rectange = shape(20,30)

I am getting NameError exceptions when i am trying to achieve these.

  python third.py
Traceback (most recent call last):
   File "third.py", line 3, in 
 class shape:
   File "third.py", line 14, in shape
 print "The values are %d,%d,%d" %(self.x,self.y,self.z)
NameError: name 'self' is not defined




can we have two constructs within the same class. My requiment is very
simple, when i want to pass two values the shape class should take two
arguments and when i pass on three it should take 3 arguments . Is this
possible ?


No, its not possible in Python, there is no overloading of methods.

The normal way to accept variable numbers of parameters is to use 
default values


def __init__(self, a, b, c=None):
if c is None:
   # do one thing
else:
   # do another

But in your case you should probably be defining subclasses since 
triangle and rectangle are probably going to need different method

implementations for most things.

The name error however is nothing to do with that.
The problem there is that you have put the print statement
outside of the method so it gets executed during class definition.
At that stage there is no instance and therefore no self value.
I suspect you wanted to have it indented to the same
level as the self assignment lines?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor