← Projects

understand classes

ee84df17e0ca60a05820374709639488424e7f0d · ovidiucs · May 21, 2015 23:43

+36 · 1 file changed

  • 00-Undertanding-Classes.py +36 −0
diff --git a/00-Undertanding-Classes.py b/00-Undertanding-Classes.py
new file mode 100644
index 0000000..4095c54
--- /dev/null
+++ b/00-Undertanding-Classes.py
@@ -0,0 +1,36 @@
+__author__ = 'ovidiucs'
+class MyClass:
+    """A simple class"""
+    i = 12345
+
+    data = [2,3,4,5,6]
+    def __init__(self):
+        """Methods"""
+        self.data = [1,2,3,4,5]
+
+    def f(self):
+        return "Hello World"
+
+x = MyClass()
+y = MyClass
+
+print x.__doc__, x.data, x.f()
+# instanta
+print type(x)
+# classobject
+print type(y)
+
+class initargu:
+    def __init__(self, realpart, imagipart):
+        self.r = realpart
+        self.i = imagipart
+
+a = initargu(3.0,-4.5)
+# instance, float, float, 3.0, -4.5
+print type(a), type(a.r), type(a.i), a.r, a.i
+
+# class initargu has no attribute 'r'
+# print initargu.r
+
+# TypeError: __init__() takes exactly 3 arguments (1 given)
+# print initargu().r
\ No newline at end of file