Python __init__() method & __init__.py file All In One

xgqfrms / 2023-04-28 / 原文

Python init() method & init.py file All In One

__init__() method

classobjects

Class objects support two kinds of operations: attribute references and instantiation.
类对象支持两种操作:属性引用实例化

  1. Attribute references use the standard syntax used for all attribute references in Python
class Human:
  """Human class"""
  name = "xgqfrms"
  age = 18
  list = [1, 2 , 3]
  def getName(self):
    return self.name
  def getAge(self):
    return self.age
  def getList(self):
    return self.list

# 实例化 class
person = Human()

print("name =", person.getName())
print("age =", person.getAge())
print("list =", person.getList())

"""
name = xgqfrms
age = 18
list = [1, 2, 3]
"""

# python3 ./classes/class-attributes.py

image

  1. Class instantiation uses function notation.
    类实例化使用函数注解函数表示法

returns a new instance of the class.
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly created class instance.

#!/usr/bin/env python3
# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
  Copyright (c) 2012-2050, xgqfrms;
  mailto:xgqfrms@xgqfrms.xyz
"""

class Human:
  # 类似 ES6 class 的 constructor 构造函数
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.list = [1, 2 , 3]

# 实例化 class
person = Human("xgqfrms", 18)

print("name =", person.name)
print("age =", person.age)
print("list =", person.list)

"""
name = xgqfrms
age = 18
list = [1, 2, 3]
"""

# python3 ./classes/class-init.py

image

https://docs.python.org/3/tutorial/classes.html#class-objects

https://docs.python.org/zh-cn/3/tutorial/classes.html#class-objects

__init__.py file

docs

https://docs.python.org/3/tutorial/

https://docs.python.org/3/contents.html

https://wiki.python.org/moin/IntroductoryBooks
https://wiki.python.org/moin/BeginnersGuide/Examples

https://docs.python.org/3/search.html?q=init

https://docs.python.org/3/reference/datamodel.html#object.init

https://docs.python.org/3/library/dataclasses.html?highlight=init#re-ordering-of-keyword-only-parameters-in-init

demos

python functions sum() & zip()

#!/usr/bin/env python3
# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
  Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
"""

list1 = [1, 3, 5]
list2 = [2, 4, 6]

# zip() & sum()
total = sum(x * y for x, y in zip(list1, list1))

print("total =", total)

print("\n")

for x, y in zip(list1, list1):
  print("x,y =", x, y)

"""
total = 35

x,y = 1 1
x,y = 3 3
x,y = 5 5

"""

# python3 ./classes/zip.py

https://docs.python.org/3/tutorial/classes.html#private-variables

https://docs.python.org/3/tutorial/classes.html#generator-expressions

zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象;
这样做的好处是节约了不少的内存;
我们可以使用 list() 转换来输出列表;
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表;

https://www.runoob.com/python/python-func-zip.html
https://www.runoob.com/python3/python3-func-zip.html

https://www.freecodecamp.org/chinese/news/python-zip-zip-function-in-python/

python tutorial

https://docs.python.org/3/tutorial/

https://docs.python.org/zh-cn/3/tutorial/

https://www.python.org/about/gettingstarted/

https://docs.python.org/3/faq/

https://wiki.python.org/moin/BeginnersGuide/NonProgrammers

https://wiki.python.org/moin/PythonEditors

Visual Studio Code

https://code.visualstudio.com/docs/languages/python

https://marketplace.visualstudio.com/items?itemName=ms-python.python

https://code.visualstudio.com/docs/python/python-tutorial

Django

https://code.visualstudio.com/docs/python/tutorial-django

https://github.com/microsoft/python-sample-vscode-django-tutorial

Flask

https://code.visualstudio.com/docs/python/tutorial-flask

https://github.com/microsoft/python-sample-vscode-flask-tutorial

refs

https://www.runoob.com/note/42127

https://www.zhihu.com/question/46973549

https://www.madlabai.com/zh/self-init-python-class/

https://www.udacity.com/blog/2021/11/init-in-python-an-overview.html

https://www.geeksforgeeks.org/init-in-python/

https://www.w3schools.com/python/gloss_python_class_init.asp



©xgqfrms 2012-2021

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!