Detailed explanation of common factory function usage in Python

小云云
Release: 2018-03-29 13:50:23
Original
1875 people have browsed it

This article mainly introduces the usage of common factory functions in Python. It briefly describes the functions and definitions of factory functions and analyzes the related usage skills of common factory functions in Python based on specific examples. Friends who need it can refer to it. I hope it can help. to everyone.

Factory function: Built-in function that can generate class instances.

Factory function means that these built-in functions are all class objects. When they are called, a class instance is actually created.

Examples of factory functions in python are as follows:

1》int(),long(),float(),complex(),bool()


>>> a=int(9.9)
>>> a
9
>>> b=long(45)
>>> b
45L
>>> f=float(8)
>>> f
8.0
>>> c=complex(8)
>>> c
(8+0j)
>>> b1=bool(7.9)
>>> b1
True
>>> b2=bool(0.0)
>>> b2
False
>>> b3=bool([])
>>> b2
False
>>> b4=bool((34,5))
>>> b4
True
Copy after login

2》str(),unicode()


>>> s=str(9.9)
>>> s
'9.9'
>>> unicode(9.0)
u'9.0'
>>> unicode('love')
u'love'
Copy after login

3》list(), tuple(): Generate list or tuple


##

>>> l=list('python')
>>> l
['p', 'y', 't', 'h', 'o', 'n']
>>> t=tuple('python')
>>> t
('p', 'y', 't', 'h', 'o', 'n')
Copy after login

4》type(): View type


>>> type(6)
<type &#39;int&#39;>
>>> type(&#39;python&#39;)
<type &#39;str&#39;>
>>> type(u&#39;love&#39;)
<type &#39;unicode&#39;>
>>> class A():
...   pass
...
>>> a=A()
>>> type(a)
<type &#39;instance&#39;>
>>> type(A)
<type &#39;classobj&#39;>
Copy after login

5》dict(): Generate a dictionary


>>> dict()
{}
>>> dict(one=1,two=2)
{&#39;two&#39;: 2, &#39;one&#39;: 1}
>>> dict(zip((&#39;one&#39;,&#39;two&#39;),(1,2)))
{&#39;two&#39;: 2, &#39;one&#39;: 1}
>>> dict([(&#39;one&#39;,1),(&#39;two&#39;,2)])
{&#39;two&#39;: 2, &#39;one&#39;: 1}
>>> dict([[&#39;one&#39;,1],[&#39;two&#39;,2]])
{&#39;two&#39;: 2, &#39;one&#39;: 1}
>>> dict(((&#39;one&#39;,1),(&#39;two&#39;,2)))
{&#39;two&#39;: 2, &#39;one&#39;: 1}
>>> dict(([&#39;one&#39;,1],[&#39;two&#39;,2]))
{&#39;two&#39;: 2, &#39;one&#39;: 1}
Copy after login

6》 set(): Produce a mutable collection

##

>>> s=set(&#39;python&#39;)
>>> s
set([&#39;h&#39;, &#39;o&#39;, &#39;n&#39;, &#39;p&#39;, &#39;t&#39;, &#39;y&#39;])
>>> s.add(825)#可变集合
>>> s
set([&#39;h&#39;, &#39;o&#39;, &#39;n&#39;, &#39;p&#39;, &#39;t&#39;, &#39;y&#39;, 825])
Copy after login

7》frozenset(): Generate an immutable collection

>>> s=frozenset(&#39;python&#39;)
>>> s
frozenset([&#39;h&#39;, &#39;o&#39;, &#39;n&#39;, &#39;p&#39;, &#39;t&#39;, &#39;y&#39;])
>>> s.add()#不可变集合
AttributeError: &#39;frozenset&#39; object has no attribute &#39;add&#39;
Copy after login

Related recommendations:

Detailed explanation of class, constructor, and factory functions in Javascript

The above is the detailed content of Detailed explanation of common factory function usage in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template