Python Web Framework Enterprise actual combat (sixth issue) \ Second Class-pickle&__eq__

Source: Internet
Author: User

1, Python value passing and reference passing differences, what type values are passed, and which are reference passes? Value passing and reference passing differences: depending on whether the object is mutable and different from other languages, Python does not allow programmers to choose to pass a value or pass a reference when passing parameters. Python parameter passing is definitely a way to "pass an object reference". In fact, this approach is equivalent to a synthesis of value-passing and reference. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference". If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value". Python is a general internal assignment variable, it is to pass a reference variable, and the C language is similar to the concept of the address. The memory address can be queried with ID () if a=b, the address of A and B is the same; if you just want to copy it, you have to use a=b[:]. #-*-coding:cp936-*-#值传递def F1 (aa): Aa=1 print "id (aa) =", id (AA) print AA bb=123print "id (bb) =", id (BB) F1 ( BB) Print bb# Reference pass def f2 (AA): aa[0]=[1] print "id (aa) =", id (AA) print AA bb=[123]print "id (bb) =", id (BB) f2 (BB) pr int bb>>> ID (bb) = 30778664id (aa) = 307781441123id (BB) = 44761936id (aa) = 44761936[[1]][[1]]>>>2, Use Python to write a command program that stores information about a number of user members that users can view only after they log in. That is: The user launches the Python script, and then enter the user name password login successfully, use the command to view other user information #-*-coding:cp936-*-class MyUser (object): Def __init__ (Self,username, Password): Self.username=username self.password=password def __del__ (self): Pass def __sTr__ (self): return str (' username=%s,password=%s '% (Self.username,self.password)) def __eq__ (Self,other): Return self.username==other.username and Self.password==other.password import pickledir (pickle) user1=myuser (' Karli Ao ', ' 123456 ') user2=myuser (' Test ', ' 123 ') Fa=open (' E:/aa.txt ', ' W ') Pickle.dump (user1,fa,2) # #0, text form, 1,2->        Binary form pickle.dump (user2,fa,2) fa.close () fr=open (' E:/aa.txt ', ' R ') Users=[]while True:try:user=pickle.load (FR) Users.append (user) except Eoferror,e:breakusername=raw_input (' Please input your username: ') password=raw_inpu T (' Please input your password: ') curuser=myuser (Username,password) if curuser in users:for user in Users:print u ser>>> Please input your username:testplease input your password:123username=karliao,password=123456username =test,password=123>>> ================================ RESTART ================================>> > Please input your username:testplease input your Password:111>>>--eof-- 

Python Web Framework Enterprise actual combat (sixth issue) \ Second Class-pickle&__eq__

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.