- Python
- Storage
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
- Json
- Class
- Definition ♣
- Attributes
- Functional
- Methods
- Goodies
- Conditional Expression
- List Comprehension
- Generator
- Named Tuple
- Modules
- Applications
- Pythagorean Theorem
- Palindrome
- Word Search
- Conway Game
- Coin Flip
- Strings
- Regex
- Encrypt
- Scheduler
- Time
- Multithreading
- Subprocess
- Logging
- Packages
- Clipboard
- Ocr
- Socket
- Image
- Virtualenv
- Jupyter
- Icecream
- Anytree
- Openai
Definition
A programmer-defined type is called a class.
# Class definition
#
# A programmer-defined type is called a class.
# A class definition cannot be empty.
class Point:
""" a 2D point """ # body docstring comment
class Point:
def get():
return (1, 1) # tuple
assert Point.get() == (1, 1)
assert Point.get() != None
p = Point()
assert Point.get() == (1, 1)
assert Point.get() != None
Factory
A class is like a factory for creating objects.
# A class is like a factory for creating objects.
# Because Point is defined at top level, its name contains __main__
class Point:
""" a 2D point """
p = Point()
print(p) # __main__.Point object
Dict
Like most Python objects, an empty class has a dictionary by default.
# An empty class has a dictionary that ...
# holds the attributes of the object.
class A(object):
pass
A = A()
A.__dict__ = {
'key11': 1,
'key2': 2,
}
A.__dict__['key2'] = 3
print(A.__dict__['key2']) # 3
Last update: 55 days ago