new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
38
Task/Classes/Python/classes.py
Normal file
38
Task/Classes/Python/classes.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
class MyClass:
|
||||
name2 = 2 # Class attribute
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Constructor (Technically an initializer rather than a true "constructor")
|
||||
"""
|
||||
self.name1 = 0 # Instance attribute
|
||||
|
||||
def someMethod(self):
|
||||
"""
|
||||
Method
|
||||
"""
|
||||
self.name1 = 1
|
||||
MyClass.name2 = 3
|
||||
|
||||
|
||||
myclass = MyClass() # class name, invoked as a function is the constructor syntax.
|
||||
|
||||
class MyOtherClass:
|
||||
count = 0 # Population of "MyOtherClass" objects
|
||||
def __init__(self, name, gender="Male", age=None):
|
||||
"""
|
||||
One initializer required, others are optional (with different defaults)
|
||||
"""
|
||||
MyOtherClass.count += 1
|
||||
self.name = name
|
||||
self.gender = gender
|
||||
if age is not None:
|
||||
self.age = age
|
||||
def __del__(self):
|
||||
MyOtherClass.count -= 1
|
||||
|
||||
person1 = MyOtherClass("John")
|
||||
print person1.name, person1.gender # "John Male"
|
||||
print person1.age # Raises AttributeError exception!
|
||||
person2 = MyOtherClass("Jane", "Female", 23)
|
||||
print person2.name, person2.gender, person2.age # "Jane Female 23"
|
||||
Loading…
Add table
Add a link
Reference in a new issue