Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
about cmp fun
#1
why cmp() fun wll not work in python 3?
Reply
#2
from Guido:
What’s New In Python 3.0, see the last bullet in Ordering Comparisons section

Quote:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
Reply
#3
dict = {'Name': 'Zara', 'Age': 7}
print ("Value :" , dict.items())
if i run the above code i supposed to get= Value :([('Name', 'Zara'), ('Age', 7)]).
But i am getting it as=Value : dict_items([('Name', 'Zara'), ('Age', 7)])..
Help me out...
Reply
#4
(Jan-12-2018, 10:29 AM)Sandesh Wrote: dict = {'Name': 'Zara', 'Age': 7}
print ("Value :" , dict.items())
if i run the above code i supposed to get= Value :([('Name', 'Zara'), ('Age', 7)]).
But i am getting it as=Value : dict_items([('Name', 'Zara'), ('Age', 7)])..
Help me out...

Firstly please use bbcode to wrap you code around. You have better chances of getting help, than one without it.

Quote:
dict = {'Name': 'Zara', 'Age': 7} 
print ("Value :" ,  dict.items()) 
if i run the above code i supposed to get= Value :([('Name', 'Zara'), ('Age', 7)]).
But i am getting it as=Value : dict_items([('Name', 'Zara'), ('Age', 7)])..
Help me out...

And I suspect that your getting this output Value : dict_items([('Name', 'Zara'), ('Age', 7)]). Is because how the __repr__ magic method had been implemented for dict class in python3. In Python2 the output is what you expect. However this has changed in python

>>> dict = {'Name': 'Zara', 'Age': 7} 
>>> dict
{'Age': 7, 'Name': 'Zara'}
>>> dict.items()
[('Age', 7), ('Name', 'Zara')]
Reply
#5
in python3 dict.items() method returns Dict View objects. In python2 same method will return list of 2-element tuples.
So, you need to pass the dict view object to list() function in order to convert it to list (what you expect)
>>> d = {'a':1, 'b':2}
>>> d.items()
dict_items([('b', 2), ('a', 1)])
>>> list(d.items())
[('b', 2), ('a', 1)]
However there is much serious problem in your code. Never use built-in functions as names, i.e. your dict name overwrite the built-in dict function:
>>> my_dict = dict((('a',1), ('b',2)))
>>> my_dict
{'b': 2, 'a': 1}
>>> dict = {'Name': 'Zara', 'Age': 7}
>>> my_dict = dict((('a',1), ('b',2)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not callable
Reply
#6
fo = open("foo.txt", "rw+") 
print ("Name of the file: ", fo.name)  
line = fo.readlines() 
print ("Read Line: %s" % (line))  
# Again set the pointer to the beginning 
fo.seek(0, 0) 
line = fo.readline() 
print ("Read Line: %s" % (line))  
# Close opened file 
fo.close()



If i run the above code I should get result as
Name of the file: foo.txt
Read Line: ['This is 1st line\n', 'This is 2nd line\n', 'This is 3rd line\n', 'This is 4th line\n', 'This is 5th line']
Read Line: This is 1st line

But i am getting something like
Name of the file: foo.txt
Read Line: []
Read Line:

Even though my txt file containing info....????
Reply
#7
actually,
you are getting
Error:
Traceback (most recent call last): File "/home/***/***.py", line 2, in <module> fo = open("foo.txt", "rw+") ValueError: must have exactly one of create/read/write/append mode >>>
Reply
#8
(Jan-23-2018, 06:22 AM)Sandesh Wrote: Even though my txt file containing info....????
You are opening file for read and write,open for read 'r' which is the default mode.
Here a better way with with open and new string formatting f-string.
with open('in.txt') as f:
    contend = [i.strip() for i in  f]

print(f'Name of file: {f.name}')
print(f'First line: {contend[0]}')
print(f'Read lines: {contend}')
Output:
Name of file: in.txt First line: line 1 Read lines: ['line 1', 'line 2', 'line 3', 'line 4', 'line 5']
Reply
#9
class Employee:
   def _init_(self,first,last,pay):
      self.first=first
      self.last=last
      self.pay=pay
      self.number=number
emp1=Employee('corey','schafer',50000)
emp1=Employee('test','user',60000)

print(emp1.first)
print(emp1.last)
print(emp1.pay)
when i try to run the above code it shows type Error saying that object() takes no parameter....!!!!!?????
Reply
#10
Two underscore __init__.
Indentation is 4-space not tree.
class Employee:
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        #self.number = number # Not used

emp1 = Employee('test', 'user', 60000)
print(emp1.first)
print(emp1.last)
print(emp1.pay)
Output:
test user 60000
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020