Little Turtle Python problematic point

Source: Internet
Author: User
Tags decimal to binary shallow copy

1.python Generator

2.while 1:

num = input (' Please enter an integer (enter Q to end the program): ')

If num! = ' Q ':

num = int (num)

Print (' decimal 16:%d-0x%x '% (num, num))

Print (' Decimal, octal:%d-0o%o '% (num, num))

Print (' decimal-to-binary:%d, '% num, bin (num))

Else

Break

how decimal to binary is implemented

3.

the explanation at the time: The big guy said --for example, the value of the function calculation is used outside the function, then you must add return

4.

The big Guy explained . :return statement [expression] exits the function, optionally returning an expression to the caller. A return statement without a parameter value returns none. Returns none by default without return. With return is the expression that returns after return. None is a value that cannot be assigned to an rvalue. The Funy is just a function object , because there is a () behind Funy, which means that funy is called.

My understanding : There is a return time to call the function and return the function a value, there is no return when it just call this function, the return value is the default of None. All have the meaning of the end function.

*, use dir (classname) to see what's in the class.
Use dir (instance) to see what's in the instance.
use __dict__ to see what properties are available
See which properties are the same with ID ()
I came to the conclusion through a series of tests:
all of the things in the class "copy" instances include class functions, static functions, class variables, which can actually invoke class variables of the same name through an instance
Instance__class__.value
In addition , self is used for binding, which can be seen by directly printing the function name to see which class object is bound
There are also inheritance, and you can use the above method to look at what is in these parent subclasses: In fact, all the things in the parent class are copied to the subclass, it is the display binding to facilitate the use of the subclass instance call the Parent class method: The child class should be bound to the parent class function
In the final analysis, it is Python's design philosophy is better, can use some methods to clearly see all things, compared to Java can only look at the data to see what kind of things in the class, inheritance, when the creation of the instance when the change occurred.

*, The relationship between classes in Python can be vertical, using the inheritance mechanism to use all the properties and methods in the base class, but the parent class can not use the methods and properties of the subclass, such as fish and sharks, carp salmon, and so on; Of course, there can be horizontal relationships between classes, You can implement call relationships between classes by instantiating an object of a class as another class of properties, such as the relationship between fish and turtles in ponds and ponds, for example:
>>> class Turtle:
def __init__ (self,x):
self.x = X
>>> class Fish:
def __init__ (self,y):
Self.y = y
>>> class Pool:
def __init__ (self,x,y):
Self.turtle = Turtle (x) #将Turtle类实例化的对象turtle作为类Pool实例化对象的属性
Self.fish = Fish (y) #将Fish类实例化的对象fish作为类Pool实例化对象的属性
def printnum (self):
Print (' There are%d turtles in the pond, there are%d of the small fish! '% (SELF.TURTLE.X,SELF.FISH.Y))
>>> pool = Pool (2,10)
>>> Pool.printnum ()
the turtle in the pond has2, there are 10 small fish!

*、

Explain:

Explanation: represents false, but not equal to false

* modifier (adorner)

* Small turtle: class Capstr (str):-----"

def __new__ (cls,string):

String=string.upper ()

Return str.__new__ (cls,string)

error: class Capstr (str):

def __new__ (cls,string):

string=string.upper ()

return str.__new__ (cls,string)

def __init (self,string):

self.x=string

Cs=capstr (' I Love You ')

Cs.x

Explanation: The Little turtle here is just an example of the use of this particular method of __new__ (CLS). In both the video and the book, it is clear that most situations do not need to rewrite this particular method. The class needs to be rewritten only if it inherits from another immutable class.
1. Class Capstr (str) Please note that this class inherits from the Str class;
2, therefore, if you do not rewrite the __new__ method, any string can not be implemented in the __init__ method of the initial capitalization of the initialization operation;
3, after the completion of the rewrite must be submitted to the parent class __new__ method to implement the return, examples are used: str (). __new__ (CLS, string) to implement, but it is generally better to use the Super function to avoid diamond inheritance.

As for your second piece of code cannot be implemented there are two places to understand the error:
1, __init__ less two underline;
2, __new__ is used to initialize the class, __init__ is used to initialize the class object, so __init__ inside the self.x attribute is CS, that is, the first pass in the I love you string, so you output or the original look.
3, only need to enter CS and return to see all uppercase strings, this is because when instantiating the object CS __new__ method In addition to the CLS parameters, other parameters will be passed to the __init__ method, so you do not need to write again. All the __init__ you have written are not affected.

You see my running results, the first one is __init__, the second one is not __init__. You can try it too!

*① __metaclass__=type

Class Capstring (str):

def __new__ (cls,string):

String=string.upper ()

Return str.__new__ (cls,string)

class capstring (str):

def __new__ (cls,string):

Return String.upper ()

Explanation:return String.upper () is returning the calculated results yourself
Return str.__new__ (cls,string) is the result of giving the parent class str processing to return (this is to prevent unnecessary errors from occurring)
and look at __new__ 's instructions.
1. __new__ is the first method that is called when an object is instantiated
2. Its first parameter is this class, and the other parameters are used to pass directly to the __init__ method
3. __new__ decides whether to use the __init__ method, because __new__ can invoke the constructor of another class or return the other instance object directly as an instance of this class, __init__ will not be called if the __new__ does not return an instance object
4. __new__ is primarily used to inherit an immutable type such as a tuple or string

*

*

*, How to change the name in the shell

explain : The instance object of the class Record () has no return value, so if it is written , x is a property of the class and has been assigned at the time of creation, and the assigned value is the return value.

In this way, this x is already ' hello ', but ' hello ' is not a property of name,

initialization This x points to the class record () , when the self.x is a class record () instance object, so there is the name attribute.

*

Explanation: shallow copy, shallow copy copy only the parent object, do not copy the inside sub-object.

Http://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html

*

explanation : ip=="119.113.112.332

*

Explanation: How many sets of parentheses are there in an expression? Only one group. the + number is outside the brackets. Parentheses match up to one character, either a, or C, andre in a single search if multiple groupings are hit, since the last group is reserved. He actually matched a,b,c, but C . Is the last match, and because only one character can be matched,C again covers the front, so C.

Extended:

M.group () = = M.group (0) This returns the entire matched string "abc"

Little Turtle Python problematic point

Related Article

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.