Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
something wrong: class
#1
the code:
from __future__ import print_function
total = 1
class Classy:
    total = 0
    def __init__(self):
        return
    def add(self,a):
        p = self.total
        self.total += a
        return p
if __name__ == '__main__':
    c = Classy
    print('c =',repr(c))
    print(dir(c))
    for n in range(3):
        x = c.add(8)
        print('x =',repr(x))
i ran it under python3:
Output:
lt1/forums /home/forums 48> py3 min1.py c = <class '__main__.Classy'> ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'total'] Traceback (most recent call last):   File "min1.py", line 16, in <module>     x = c.add(8) TypeError: add() missing 1 required positional argument: 'a' lt1/forums /home/forums 49>
what did i miss in learning about classes?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
The constructor for c must be:
c = Classy()
Reply
#3
DO'H!!! Wall   and i know that, too.  i need more class coding experience to recognize such mistakes.  now on to trying to figure out my other coding goof.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Class Instances called in the wrong order IanIous 4 2,777 Mar-06-2020, 02:16 PM
Last Post: IanIous
  python gives wrong string length and wrong character thienson30 2 2,945 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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